Loading takes a while when i set UIImage to a NSData with a url.

6 Solutions Collect From Internet About “Loading takes a while when i set UIImage to a NSData with a url.”

use sdwebimage and down load the library file from here

the few steps you do follow

add the sdwebimage in your project after that

in your .h file

#import "UIImageView+WebCache.h"

in your .m file

call the single line in your cellforRowAtIndexPath

[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageUrl"]]
                                     placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

I would suggest you to use AsyncImageView a beautiful implementation by Nicklockwood -father of iCarousel.

it is very simple to use.

    #import "AsyncImageView.h"

and in all imageViews do this.

    [imageView setImage:@"default.png"];
    [imageView setImageURL:@"whateverurl/whateverimage.png"];

In your case it would be:

    [cell.thumbnailImageView setImageURL:@"yourURL"];

It works like a charm, and my code is in production. But if you still want your code to work try this:

 UIActivityIndicator *activity =[[UIActivityIndicator alloc] initWithStyle:UIActivityIndicatorWhite];
 [activity setFrame:CGRectMake(0,0,30,30)];
 [cell.contentView addSubview:activity];

 cell.thumbnailImageView.image=[UIImage imageNamed:@"Default~cell~image.png"];      

 dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
 dispatch_async(dispatchQueue, ^(void)
   [activity startAnimating];
   [self loadImages];
   dispatch_sync(dispatch_get_main_queue(), ^{ 
           NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageUrl"] ]];
           cell.thumbnailImageView.image=[UIImage imageWithData:imageUrl];          [activity stopAnimating];
           [activty setHidden:YES];

This happens because every time your cell goes off screen your image is released and if you scroll it back on screen you will have to download your image again. Simply cache your image when downloaded and check if exists use it if not download from internet. You can use third party library like SDWebImage GitHub link

Import SDWebImageView+WebCache.h and in your cellForRowAtIndexPath use the following

[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageUrl"] placeholder:[UIImage imageNamed:@"placeholder.png"]];

Use an NSOperationQueue so that you can perform the loading of the data on a background thread.
After that you should set the image on the main queue.

// Assume queue is created
[queue addOperationWithBlock:^{
    NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageUrl"] ]];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        cell.thumbnailImageView.image=[UIImage imageWithData:imageUrl];

For more about NSOperationQueue see the docs about NSOperationQueue.

So try to load image asynchronous.


Use gcd to populate the images,

NSURL *imageUrl = [NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageUrl"] ]];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
   UIImage *image = nil;
   image = [UIImage imageWithData: [NSData dataWithContentsOfUrl: imageUrl]];
  dispatch_sync(dispatch_get_main_queue(), ^{
   cell.thumbnailImageView.image = image;

You will probably want to store the image to some model object, also track if the download has been initiated, since you would not want to queue up the request for the same image url.