7

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

Adrian
  • 19,440
  • 34
  • 112
  • 219

5 Answers5

5

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;
}
The dude
  • 7,896
  • 1
  • 25
  • 50
  • 1) It not tracking the imageview so not putting the images on correct imageviews on scrolling. 2) Images get off on scrolling and shows up after sometime again. Seems not using Cache – Developer Jun 17 '14 at 11:19
  • About number 2, SDWebImageDownloader caches images by default. – The dude Jun 17 '14 at 13:49
  • 1
    Yes but images should not placed incorrectly even if they are coming from cache! Check your code by your self! 2) issue is still there! – Developer Jun 18 '14 at 05:57
1

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];
                            }
                        }];
1

This probably has less to do with network activity and more to do with your image sizes. Resizing images, especially for non-integer multipliers, is expensive. If your images are significantly larger than the view you are putting them into, you need to resize and cache them in a background thread, and reference the cached copy from your view.

To confirm if this is your issue, manually download all the images and reference the local copy. If I am correct, your UITableView will still lag the same way.

Holly
  • 5,270
  • 1
  • 24
  • 27
0

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.

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

Community
  • 1
  • 1
Pratik
  • 2,399
  • 17
  • 36
0

Check you images, check the types and size and more important: Is one or more image broken by its file format in any way? Filesize too big, irregular (to large) bounds?

Try to open and re-save suspiciously image files with an image editor, to be sure the internal file format is OK.

ThorstenC
  • 1,264
  • 11
  • 26