I personally use the built-in Grand Central Dispatch feature in iOS to asynchronously download images from the server.
Below is the code that I used to retrieve photos from Flickr in one of my applications.
There is a function in your image / photo class that looks something like this:
- (void)processImageDataWithBlock:(void (^)(NSData *imageData))processImage { NSString *url = self.imageURL; dispatch_queue_t callerQueue = dispatch_get_current_queue(); dispatch_queue_t downloadQueue = dispatch_queue_create("Photo Downloader", NULL); dispatch_async(downloadQueue, ^{ NSData *imageData = *insert code that fetches photo from server*; dispatch_async(callerQueue, ^{ processImage(imageData); }); }); dispatch_release(downloadQueue); }
In your photo view controller, you can call this function as follows:
- (void)viewWillAppear:(BOOL)animated { [spinner startAnimating]; [self.photo processImageDataWithBlock:^(NSData *imageData) { if (self.view.window) { UIImage *image = [UIImage imageWithData:imageData]; imageView.image = image; imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height); scrollView.contentSize = image.size; [spinner stopAnimating]; } }]; }
source share