Question

XCode 4.5.2; I'm downloading an image from a remote server like this :

- (void)viewDidLoad
{
[super viewDidLoad];

NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                    initWithTarget:self
                                    selector:@selector(loadImage)
                                    object:nil];

[queue addOperation:operation];
}

- (void)loadImage{
self.theobject = [RemoteQuery loadObjectWithImage:self.imageKey];

[self performSelectorOnMainThread:@selector(displayImage) withObject:nil waitUntilDone:YES];
}

-(void)displayImage{
UIImage *image = [UIImage imageWithData: self.theobject.imageData];
[self.imageView setImage:image];

}

This works fine on IOS simulator, but doesn't work on a device; it seems like displayImage is called before the data is loaded from [RemoteQuery loadImage]. What would be the best way to ensure that the image has loaded properly before showing it ?

Was it helpful?

Solution

Create a delegate protocol which will call back to the original object when the image download finishes. This NSOperation tutorial has more details on how to do this

Alternatively, use the NSOperation's completionBlock to perform the image display.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top