Table scroll view delay when using SDWebImage

I am downloading some images from the Internet as a table 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 application is still lagging. Here are some screenshots from Instruments :

enter image description here

enter image description here

+7
ios sdwebimage
source share
5 answers

It seems that even if the image is being downloaded in the background thread, the image data is being processed in the main thread, so it blocks your application. You can 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 this:

 - (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; } 
+5
source share

Upload the image in a separate stream, for example:

 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
source share

This is probably due not so much to network activity as to the size of your images. Resizing images, especially for non-integer factors, is expensive. If your images are significantly larger than the view into which you paste them, you need to resize and cache them in the background thread and reference the cached copy from your view.

To confirm that this is your problem, manually download all the images and provide a local copy. If I'm right, your UITableView will still lag.

+1
source share

You must load asynchronously from the server so that your TableView scrolls smoothly.

Please check below answer that you can solve your problem.

stack overflow

0
source share

Check the images, check the types and sizes and, more importantly: is any image broken by its file format? File size too large, irregular (to large) borders?

Try opening and re-saving suspicious image files in the image editor to make sure the internal file format is in order.

0
source share

All Articles