Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table view scroll lagging when using SDWebImage

Tags:

ios

sdwebimage

I am loading some images from the internet in a table view inside cellForRowAtIndexPath. Here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"ArticleCell";
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    Article *article = [parser items][indexPath.row];

    cell.title.text = article.title;
    cell.newsDescription.text = article.description;
    [cell.image setImageWithURL:[NSURL URLWithString:article.image]];

    return cell;
}

My problem is that even if I use SDWebImage, when I scroll down, my app still lags. Here is some screenshots from Instruments:

enter image description here

enter image description here

like image 891
Adrian Avatar asked Oct 13 '13 08:10

Adrian


1 Answers

It looks like even though the download of the image is performed in a background thread, the work with the image data is done in the main thread, thus it blocks your application. You could try the asynchronous image downloader provided by SDWebImage.

[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
                                                    options:0
                                                   progress:^(NSUInteger receivedSize, long long expectedSize)
                                                   {
                                                       // progression tracking code
                                                   }
                                                   completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
                                                   {
                                                       if (image && finished)
                                                       {
                                                           // do something with image
                                                       }
                                                   }
];

In your method it should look like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"ArticleCell";
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    Article *article = [parser items][indexPath.row];

    cell.title.text = article.title;
    cell.tag = indexPath.row;
    cell.newsDescription.text = article.description;
    [SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
                                                        options:0
                                                       progress:nil
                                                      completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
     {
         if (cell.tag == indexPath.row && image && finished)
         {
            dispatch_async(dispatch_get_main_queue(), ^(){
               cell.image = image;
            });

         }
     }];

    return cell;
}
like image 103
The dude Avatar answered Oct 02 '22 07:10

The dude