Pangu
Pangu

Reputation: 3819

What is the more efficient way of loading an image from the web in a UITableView?

for each UITableViewCell in my UITableView, I download an image from the web in the background thread. Then I update the imageView from the cell using the main thread.

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
                //Background Thread
                // download the image in separate thread
                UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: @"someurl.com" ] ] ];
                dispatch_async(dispatch_get_main_queue(), ^(void){
                    // this is called in the main thread after the download has finished, here u update the cell's imageView with the new image
                    cell.eventImageView.image = image;
                });
            });

I dont notice any lags when scrolling.

However, I like to know if there is a better way of doing this, and if so, how can I do it?

Thanks

Upvotes: 0

Views: 2507

Answers (1)

sam_smith
sam_smith

Reputation: 6093

I prefer using SDWebImage (can be found here on Git)

I have written pages which sound similar to yours and it involves very simple code:

    // Set and load the images
    [cell.menuImageView sd_setImageWithURL:url placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        // Get rid of the activity indicator when the image has been loaded
        [activity stopAnimating];
    }];

There are, in my opinion, a number of advantages:

1) The library is flexible and gives you lots of options on how you want to download the image (these are only a few)

eg:

- (void)sd_setImageWithURL:(NSURL *)url
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
- (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock

2) The library caches the images meaning that the next time you don't have to download them again.

3) The library is well supported helping to trouble shoot issues

I personally started using the technique you used and found there were limitations and difficulties (adding activity indicators if the image was slow downloading etc) and then trialled a number of different asynchronous image downloaders and this one came out as the best

Hope it helps

Upvotes: 3

Related Questions