IOS delays loading pictures in Cell in TableView

Source: Internet
Author: User

IOS delays loading pictures in Cell in TableView

In TableView, delayed loading of images is the content to be introduced in this article. We often use tableView to display many entries, and sometimes we need to display images. However, retrieving all images from the server at one time wastes traffic on the user, which is also a burden on the server. It is best to load images as needed, that is, when the user wants to browse the entry and then load it, many entries will be displayed in tableView.

Sometimes images need to be displayed, but 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, so the response to the user is lost.

Therefore, we need to propose this method:

 
  1. -(Void) updateImageForCellAtIndexPath :( NSIndexPath *) indexPath
  2. {
  3. NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];
  4. UIImage * image = [self getImageForCellAtIndexPath: indexPath];
  5. UITableViewCell * cell = [self. tableView cellForRowAtIndexPath: indexPath];
  6. [Cell. imageView into mselecw.mainthread: @ selector (setImage :) withObject: image waitUntilDone: NO];
  7. [Pool release];
  8. }

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];
  4. }

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.

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.