views:

326

answers:

2

1.My mainView is a UIScrollView. I have to load 9 images as tiles onto it.

2.I am calling the function as a different thread to get the 9images and to load it into the interface(mainthread). By doing so, the interface remains responsive.

3.But the problem even when the images are being loaded they doesnt get displayed in the UIScrollView until the whole thread is completed. But if I am doing some action on the interface, I am able to see each of them being loaded and displayed.

  1. Means I have to update the interface(mainthread) from the background thread after each image is being loaded into it.

How can i do it? I have given the code which I use to load the images to the UIScrollView which i call using a different thread.

- (void)setUpDisplay:(NSArray *)array 
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];

myScrollView.userInteractionEnabled = YES;
myScrollView.multipleTouchEnabled = YES;

int x=0,y=0;
CGRect frame;
for (i = 0; i < 3; i++)
{
    if(j == 3)
    {
        x=x+256;
        y = y-768;
    }
    for (j = 0; j < 3; j++) {
        imageView = [[UIImageView alloc] initWithImage:[self getCachedImage:[NSString stringWithFormat:@"url to fetch images"]]];
        frame = [imageView frame];
        frame.origin.x = x;
        frame.origin.y = y;
        [imageView setFrame:frame];
        imageView.userInteractionEnabled = YES;
        imageView.multipleTouchEnabled = YES;
        [myScrollView addSubview:imageView];
        y = y+256;

        [imageView release];
        imageView=nil;
    }
}

[pool release];
}
+2  A: 

This method of NSObject is often helpful:

[someObject performSelectorOnMainThread:@selector(updateUI:) withObject:anotherObject waitUntilDone:YES /* or NO */];
chpwn
In the background thread,ie, in the function I added 1 more line. [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES]; and wrote another function - (void)updateUI { [myScrollView setNeedsDisplay]; } Still no effect.
wolverine
You want to set waitUntilDone to NO in that implementation to avoid having the background thread block while waiting for the main thread to update. I've found that this is often the problem when not seeing updates to the UI that you've caused from a background thread.
Brad Larson
A: 

You may want to try calling [myScrollView setNeedsDisplay] on the main thread after each image load is finished.

Kendall Helmstetter Gelner
In the background thread,ie, in the function I added 1 more line. [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES]; and wrote another function - (void)updateUI{ [myScrollView setNeedsDisplay];} Still no effect.
wolverine
The scroll view shouldn't need to be manually redrawn using -setNeedsDisplay if all he is doing is adding subviews to it. The subviews should appear automatically. -setNeedsDisplay is only useful if there is something in that view's -drawRect: that needs to be redrawn.
Brad Larson