Image caching for IOS tableview asynchronous loading

Source: Internet
Author: User

1. Establish a viewcontroller. H file implements Uiscrollviewdelegate and Uitableviewdelegate, and declares the ictableviewdelegate (used to implement the picture has a cache to load the picture, no cache then requests the picture and caches it and then loads it). h file as follows
#define Kimagekey @ "Photofileurl"///the  key name of the image URL for each item in the array, # define Kidkey @ "ActivityID"//    ID of each item in the array  used for caching #import <UIKit/UIKit.h> @protocol ictableviewdelegate@required    -(void) Cellimagedidload: ( Nsindexpath *) Indexpath Image: (Nsmutablearray *) Imagearray; @end @interface Ictableviewcontroller:uiviewcontroller <uiscrollviewdelegate,uitableviewdelegate>{@public    ID <WQTableViewDelegate> wqtableviedelegate;    Nsmutablearray *tabledataarray;    UITableView *wqtable;} @end

The. m file is as follows:
-(void) loadcellimage{//method implementation picture has a cache load picture, no cache then request picture and cache and then load nsarray *indexpathsforload = [Wqtable indexpathsforvisibl    Erows];        For (Nsindexpath *item in indexpathsforload) {Nsinteger Rownumberforcell = Item.row;        NSLog (@ "%li", (long) Rownumberforcell);        NSLog (@ "%li", (unsigned long) [Tabledataarray Count]);        if (Rownumberforcell >[tabledataarray count]-1) {return;        } nsstring *imagestr =tabledataarray[rownumberforcell][@ "Photofileurl"];        NSLog (@ "%@", imagestr);        Nsmutablearray *imagearray = [Nsmutablearray array];            if ([Imagestr length]!=0) {Nsarray *photourl = [Imagestr componentsseparatedbystring:multi_files_separator];  for (int i=0;i<photourl.count-1;i++) {//Display picture NSData *imagedata = [NSData Datawithcontentsofurl:[nsurl urlwithstring:[[nsstring stringwithformat:@ "%@/%@%@", [WiseApplicationViewController Getimgbucketdomain],[wiseapplicationviewcontrollER getorganizationid],photourl[i]] [stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]]; NSString *imagename = [Tabledataarray[rownumberforcell][kimagekey] stringbyappendingstring:[nsstring                stringwithformat:@ ". Temp"]; NSString *imagedatapath = [Nshomedirectory () stringbyappendingpathcomponent:[@ "library/caches/"                                Stringbyappendingstring:imagename]]; if (![ [Nsfilemanager Defaultmanager] Fileexistsatpath:imagedatapath] {[ImageData writetofil                    E:imagedatapath Atomically:yes];                                        UIImage *image = [UIImage imagewithdata:imagedata];                                                        [Imagearray Addobject:image];        }} [Ictableviedelegate Cellimagedidload:item Image:imagearray]; }}} #pragma mark-table View delegate-(void) TableView: (UITableView *) TableView Willdisplaycell: (uitableViewcell *) cell Forrowatindexpath: (Nsindexpath *) Indexpath{//<span style= "font-family: ' Courier New '; White-space: Pre-wrap; Background-color:rgb (245, 245, 245); margin:0px; padding:0px; line-height:1.5!important; " > Drag </span><span style= "LINE-HEIGHT:19PX; White-space:pre-wrap; Font-family:arial, Helvetica, Sans-serif; >tableview</span><span style= "font-family: ' Courier New '; White-space:pre-wrap; Background-color:rgb (245, 245, 245); margin:0px; padding:0px; line-height:1.5!important; " > Perform </span><span style= "line-height:19px after the deceleration is completed; White-space:pre-wrap; Font-family:arial, Helvetica, Sans-serif; > Stop scrolling when start cache load picture process </span> if (!tableview.isdragging &&!tableview.isdecelerating) {[Self    Performselectorinbackground: @selector (loadcellimage) Withobject:nil]; }} #pragma mark-scroll View delegate-(void) scrollviewdidenddecelerating: (Uiscrollview *) Scrollview{//<span style= "Font-family: ' Courier New '; WHite-space:pre-wrap; Background-color:rgb (245, 245, 245); margin:0px; padding:0px; line-height:1.5!important; " > Drag <span style= "Background-color:rgb (240, 240, 240); line-height:1.5; " >scroll</span><span style= "Background-color:rgb (240, 240, 240); line-height:19px; Font-family:arial, Helvetica, Sans-serif; >view</span><span style= "margin:0px; padding:0px; line-height:1.5!important; " > After completion of deceleration when performing </span><span style= "Background-color:rgb (240, 240, 240); line-height:19px; Font-family:arial, Helvetica, Sans-serif; > Start cache load picture process </span></span> [self performselectorinbackground: @selector (loadcellimage) Withobject: NIL];} -(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) Decelerate{//<span style= " Color:rgb (51, 51, 51); Font-family:verdana, Sans-serif, song body; font-size:13px; letter-spacing:0.5px; line-height:22.5px; " > code to execute when scrolling is stopped </span> if (!decelerate) {[Self performselectoRinbackground: @selector (loadcellimage) Withobject:nil]; }}

Then the specific subclass inherits the parent class and implements the Ictableviewdelegate proxy method
#pragma mark ictableviewdelegate-(void) Cellimagedidload: (Nsindexpath *) Indexpath Image: (Nsmutablearray *) Imagearray    {Eventshowtableviewcell *cell = (Eventshowtableviewcell *) [_eventlisttableview Cellforrowatindexpath:indexpath];                if ([Imagearray count]!=0) {for (int i=0;i<imagearray.count;i++) {if (is_ios8_or_later) { Customphotobtn *photobtn = (CUSTOMPHOTOBTN *) [Cell.contentview Viewwithtag: (i + 1) *10]//<span style= "font-family: Arial, Helvetica, Sans-serif; >customphotobtn</span><span style= "font-family:arial, Helvetica, Sans-serif;" > Load a control in a picture package </span> UIImage *thumbimg = [filetransferhelp thumbnailwithimage: (UIImage *) Imagearray                [i] Size:cgsizemake (60, 40)];                [Photobtn.imgview setimage:thumbimg];            [Cell.contentview ADDSUBVIEW:PHOTOBTN];                }else{customphotobtn *photobtn = (CUSTOMPHOTOBTN *) [Cell.contentview Viewwithtag: (i + 1) *10]; UIImage *thumbimg = [Filetransferhelp thumbnailwithimage: (UIImage *) Imagearray[i] Size:cgsizemake (60, 40)];                [Photobtn.imgview setimage:thumbimg];            [Cell ADDSUBVIEW:PHOTOBTN]; }        }    }    }
Setting the contents of each cell in a subclass.

-(uitableviewcell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

Write it down.
Customphotobtn *photobtn = [customphotobtn customphotobtn];//loading a control in the picture package    [Photobtn.filefullname SetText:url];        NSString *imagename = [url stringbyappendingstring:[nsstring stringwithformat:@ ". Temp"];        NSLog (@ "imagename%@", imageName);    NSString *imagedatapath = [Nshomedirectory () stringbyappendingpathcomponent:[@ "library/caches/" stringbyappendingstring:imagename]]//to find the image    NSLog (@ "imagedatapath%@", Imagedatapath) from the cache;//    [data Writetofile:imagedatapath Atomically:yes];    UIImage *image = [UIImage imagewithdata:[nsdata datawithcontentsoffile:imagedatapath];    UIImage *thumbimg = [Filetransferhelp thumbnailwithimage:image size:cgsizemake (.)];    if (thumbimg) {        [Photobtn.imgview setimage:thumbimg];    }        return photobtn;



Image caching for IOS tableview asynchronous loading

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.