How to solve the problem of delayed image loading in TableView during IOS development

Source: Internet
Author: User

IOS developmentHow to solveTableViewMediumImageDelayed loading is the content to be introduced in this article, mainly to learnTableViewLoadImage. For details, refer to the details in this article.

We often use tableView to display many entries. Sometimes we need to display images. However, retrieving all images from the server at a time wastes user traffic, which is also a burden on the server. it is best to load as needed, that is, when the user wants to browse the entry, then load its image.

Rewrite the following method:

 
 
  1. -(Void) tableView :( UITableView *) tableView willDisplayCell :( UITableViewCell *) cell forRowAtIndexPath :( NSIndexPath *) indexPath
  2. {
  3. UIImage * image = [self getImageForCellAtIndexPath: indexPath]; // obtain an image from the Internet
  4. [Cell. imageView setImage: image];
  5. }

This solves the problem of delayed loading, but when the network speed is very slow or the image is very large (assuming, although the figure in the cell is usually small ), you will find that the program may lose the response to the user.

The reason is:

 
 
  1. UIImage *image = [self getImageForCellAtIndexPath:indexPath];  

This method may take a lot of time, and the main thread needs to process this method.

Therefore, the response to the user is lost.

Therefore, we need to propose this method:

 
 
  1. - (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  4.     UIImage *image = [self getImageForCellAtIndexPath:indexPath];  
  5.     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];  
  6.     [cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];  
  7.     [pool release];  

And then start a new thread to do this.

 
 
  1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     [NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];  

Similarly, when we need a long period of computing, we also need to open a new thread to do this computing to prevent the program from being suspended.

The above code is just an example and can be improved more. For example, the image will be cached once it is down from the Internet and will not be downloaded when it is displayed again.

Summary:IOS developmentHow to solveTableViewMediumImageThe content of delayed loading has been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.