Asynchronous Image Downloading Using NSOperation and NSInvocation in iPhone and iPad Development

downloading an image from url ,, here is pretty straight forward method which blocks UI , if it is huge. because this is synchronous method

NSData *imageData = [NSData dataWithContentsOfURL:yourURL];
UIImage *image = [UIImage imageWithData:imageData];

So here is method to download asynchronously

dispatch_queue_t downloadQueue = dispatch_queue_create(“image downloader”, NULL);

dispatch_async(downloadQueue, ^{
[NSData dataWithContentsOfURL:photoURL];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
// Code to show the image in the UI goes here
});
});

this wont block your UI, hope it will help someone.

Leave a Reply

Your email address will not be published. Required fields are marked *