Tuesday, November 26, 2013

Table view scroll lagging when using SDWebImage http://stackoverflow.com/questions/19343356/table-view-scroll-lagging-when-using-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

asked Oct 13 at 8:47
vBx
1,70132457
 
How big are the images? –  Wain Oct 13 at 9:08
 
well, something between 50.000 and 200.000, lets say 150 KB –  vBx Oct 13 at 9:21 
 
What type are the images? TIFF? –  Wain Oct 28 at 21:18
add comment

5 Answers

up vote1down voteaccepted
+100

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.newsDescription.text = article.description;      [SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL                                                          options:0                                                         progress:nil                                                        completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)       {           if (image && finished)           {              dispatch_async(dispatch_get_main_queue(), ^(){                 cell.image = image;              });             }       }];        return cell;  }
answered Oct 30 at 13:25
The dude
2,082524
add comment

Download the image on a separate thread, like so:

  NSURLRequest *request = [NSURLRequest requestWithURL:yourURL];                          [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {                              if ( data )                              {                                  UIImage *image = [[UIImage alloc] initWithData:data];                                  [cell.image setImage:image];                              }                          }];
answered Oct 29 at 10:03
MachOSX
371411
 
@vBx said he already uses the sdWebImage extension which uses a seperated thread. –  ThorstenC Oct 30 at 13:11
add comment

You have to load asynchronously from server so your tableView will scrolling smoothly.

Please check below answer you will be able to solve your problem.

http://stackoverflow.com/a/15331306/1713478

answered Oct 30 at 13:04
Pratik
1,331313
add comment

No comments:

Post a Comment