How to solve image delay loading in iOS development in TableView

Source: Internet
Author: User

Often we will display many items with TableView, sometimes need to display pictures, but once from the server to take all the pictures to the user to waste traffic, the server is also a burden. It is best to load on demand, that is, when the user wants to browse for the item and then loads its picture.

-(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: ( Nsindexpath *) indexpath  {      *image = [self getimageforcellatindexpath:indexpath];  // get pictures  from the internet     [Cell.imageview setimage:image];    

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

Reason is

UIImage *image = [self getimageforcellatindexpath:indexpath];

This method can take a lot of time to process the main thread.

So the response to the user has been lost.

So you need to do the following rewrite:

-(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: ( Nsindexpath *) indexpath  {    [self  performselectorinbackground: @selector (  Updateimageforcellatindexpath:)];  }   -(void) Updateimageforcellatindexpath: (Nsindexpath *) indexpath  {            @autoReleasePool { c11/>*image = [self getimageforcellatindexpath:indexpath];       *cell = [Self.tableview cellforrowatindexpath:indexpath];    [Cell.imageview performselectoronmainthread: @selector (setimage:) withobject:image waituntildone:no];      

How to solve image delay loading in iOS development in TableView

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.