Loading images asynchronously in IOS UITableView-Solutions

Source: Internet
Author: User

Issue background:

You need to download the picture in each row in UITableView, before using placeholder, and the cache is available after download.


Solution:

Programme one:

Using Sdwebimage:https://github.com/rs/sdwebimage

How to install and use the code that is explained in detail in the Git page, using the specific:

#import <sdwebimage/uiimageview+webcache.h>...-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    static NSString *myidentifier = @ "Myidentifier";    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:myidentifier];    if (cell = = nil)    {        cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault                                       Reuseidentifier:myidentifier] autorelease];    }    Here we use the new provided Setimagewithurl:method to load the Web image    [Cell.imageview setimagewithurl:[nsurl U rlwithstring:@ "Http://www.domain.com/path/to/image.jpg"]                   placeholderimage:[uiimage imagenamed:@ " Placeholder.png "];    Cell.textLabel.text = @ "My text";    return cell;}


Scenario Two:

Use iOS's own asynchronous multi-threaded, download a good picture and save it to the cache, and then use the delegate function to update TableView:

-(void) Reloadrowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation


Initialization

@interface Viewcontroller () @property (Strong, nonatomic)  Nsmutablearray *imagearray; @property (Strong, Nonatomic ) Nscache *imagecache; @property (weak, nonatomic) Iboutlet UITableView *mycustomtableview; @end @implementation viewcontroller-(void) viewdidload {    [super viewdidload];        Self.imagecache = [[Nscache alloc] init];}
TableView delegate Function:

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {return 1;} -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {//number of rows is the NUM    ber of time zones in the region for the specified section. return 32;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {NSLog (@)        Cellforrowatindexpath%d ", indexpath.row);    static NSString *myidentifier = @ "Customidentifier";    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:myidentifier]; if (cell = = nil) {cell = [[Customtableviewcell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:m    Yidentifier]; } if ([Cell Iskindofclass:[customtableviewcell class]]) {Customtableviewcell *customcell = (Customtablev        Iewcell *) cell;                NSData *imagedata = [Self.imagecache objectforkey:[nsstring stringwithformat:@ "%d", Indexpath.row]]; if (imageData! = Nil) {customCell.customImageView.image = [UIImage imagewithdata:imagedata];                        } else{customCell.customImageView.image = [UIImage imagenamed:@ "Test1"]; Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default, (unsigned long) NULL), ^ (void) {U                                IImage *image = [self getpicturefromserver];                    if (image = nil) {NSData *tmpimagedata = uiimagepngrepresentation (image);                    [Self.imagecache setobject:tmpimagedata forkey:[nsstring stringwithformat:@ "%d", Indexpath.row]];                                        NSLog (@ "Download image for%d", indexpath.row); [Self Performselectoronmainthread: @selector (reloadtableviewdataatindexpath:) Withobject:indexpath WaitUntilDone:                NO];                } else{//Do nothing.        }            });    }} else{//Do nothing: }    return cell;} 
Reloadtableviewdataatindexpath:

-(void) Reloadtableviewdataatindexpath: (Nsindexpath *) indexpath{NSLog (@ "Mywineviewcontroller:in Reload Collection V    Iew data for index:%d ", indexpath.row);    nsarray* Indexarray = [Nsarray arraywithobjects:indexpath, Nil]; [Self.mycustomtableview Reloadrowsatindexpaths:indexarray withrowanimation:uitableviewrowanimationfade];//[ Self.mycustomtableview Reloadrowsatindexpaths:indexarray withrowanimation:uitableviewrowanimationautomatic];//[ Self.mycustomtableview Reloadrowsatindexpaths:indexarray withrowanimation:uitableviewrowanimationbottom];//[ Self.mycustomtableview Reloadrowsatindexpaths:indexarray withrowanimation:uitableviewrowanimationleft];//[ Self.mycustomtableview Reloadrowsatindexpaths:indexarray withrowanimation:uitableviewrowanimationmiddle];//[ Self.mycustomtableview Reloadrowsatindexpaths:indexarray withrowanimation:uitableviewrowanimationnone];//[ Self.mycustomtableview Reloadrowsatindexpaths:indexarray Withrowanimation:uitableviewrowanimationright];//[Self.mycustomtableview Reloadrowsatindexpaths:indexarray Withrowanimation:uitableviewrowanimationtop] ;}

Download image

-(UIImage *) getpicturefromserver{UIImage *image = nil; NSString *urlstring = [NSString stringwithformat:@ "Http://yourPicture<span style=" font-family:arial, Helvetica, Sans-serif; " >.</span><span style= "font-family:arial, Helvetica, Sans-serif;"    >jpg "];</span> nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] init];    [Request Seturl:[nsurl urlwithstring:urlstring];        [Request sethttpmethod:@ "GET"];    Nsurlresponse *response;    Nserror *connectionerror; NSData *returndata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&    Connectionerror];        if (Connectionerror = = NULL) {Nsinteger StatusCode = [(Nshttpurlresponse *) response StatusCode];                NSLog (@ "Getpicturefromserver-response Status code:%ld", (long) statusCode);        if (StatusCode = =) {image = [UIImage imagewithdata:returndata];        } else{//Do nothing. }    }   else{//Do nothing. } return image;

Scenario Three: (not recommended)

On the basis of scenario two, the change is to download a good picture after the load of the entire tableview, the shortcomings will cause sliding tableview when blocked.

-(void) reloadtableviewdata{    NSLog (@ "Mywineviewcontroller:in Reload Collection View Data");    [Self.mycustomtableview Reloaddata];}

Reference Links:

Http://stackoverflow.com/questions/15290974/uitableview-on-screen-image-download-lazy-loading

Http://stackoverflow.com/questions/7040740/reload-spefic-uitableview-cell-ios

Loading images asynchronously in IOS UITableView-Solutions

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.