How to asynchronously load an image in an UIImageView? How to asynchronously load an image in an UIImageView? ios ios

How to asynchronously load an image in an UIImageView?


The problem is that UIImage doesn't actually read and decode the image until the first time it's actually used/drawn. To force this work to happen on a background thread, you have to use/draw the image on the background thread before doing the main thread -setImage:. This worked for me:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{    UIImage * img = [UIImage imageNamed:@"background.jpg"];    // Make a trivial (1x1) graphics context, and draw the image into it    UIGraphicsBeginImageContext(CGSizeMake(1,1));    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), [img CGImage]);    UIGraphicsEndImageContext();    // Now the image will have been loaded and decoded and is ready to rock for the main thread    dispatch_sync(dispatch_get_main_queue(), ^{        [[self imageView] setImage: img];    });});

EDIT: The UI isn't blocking. You've specifically set it up to use UILongPressGestureRecognizer which waits, by default, a half a second before doing anything. The main thread is still processing events, but nothing is going to happen until that GR times out. If you do this:

    longpress.minimumPressDuration = 0.01;

...you'll notice that it gets a lot snappier. The image loading is not the problem here.

EDIT 2: I've looked at the code, as posted to github, running on an iPad 2, and I simply do not get the hiccup you're describing. In fact, it's quite smooth. Here's a screenshot from running the code in the CoreAnimation instrument:

enter image description here

As you can see on the top graph, the FPS goes right up to ~60FPS and stays there throughout the gesture. On the bottom graph, you can see the blip at about 16s which is where the image is first loaded, but you can see that there's not a drop in the frame rate. Just from visual inspection, I see the selection layer intersect, and there's a small, but observable delay between the first intersection and the appearance of the image. As far as I can tell, the background loading code is doing its job as expected.

I wish I could help you more, but I'm just not seeing the problem.


You can use AFNetworking library , in which by importing the category

"UIImageView+AFNetworking.m"and by using the method as follows :

[YourImageView setImageWithURL:[NSURL URLWithString:@"http://image_to_download_from_serrver.jpg"]       placeholderImage:[UIImage imageNamed:@"static_local_image.png"]               success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {                  //ON success perform                }               failure:NULL];

hope this helps .


I had a very similar issue with my application where I had to download lot of images and along with that my UI was continuously updating. Below is the simple tutorial link which resolved my issue:

NSOperations & NSOperationQueues Tutorial