iOS multi-image download, download images in cell, Cache optimizer.
(App.icon is the image address)
//Take the picture out of the memory cache firstUIImage *image =Self.images[app.icon]; if(image) {//There are pictures in memoryCell.imageView.image =image; } Else{//no pictures in memory//Get library/caches folderNSString *cachespath =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) firstobject]; //Get file nameNSString *filename =[App.icon lastpathcomponent]; //calculate the full path of a fileNSString *file =[Cachespath Stringbyappendingpathcomponent:filename]; //loading the sandbox's file dataNSData *data =[NSData Datawithcontentsoffile:file]; if(data) {//Direct use of pictures in sandboxUIImage *image =[UIImage Imagewithdata:data]; Cell.imageView.image=image; //Save in DictionarySelf.images[app.icon] =image; } Else{//Download Image[Self.queue addoperationwithblock:^{ //download pictures, multithreadingNSData *data =[NSData Datawithcontentsofurl:[nsurl URLWithString:app.icon]; UIImage*image =[UIImage Imagewithdata:data]; [Nsthread sleepfortimeinterval:1.0]; //back to main thread display picture[[Nsoperationqueue Mainqueue] addoperationwithblock:^{cell.imageView.image=image; }]; //Save in DictionarySelf.images[app.icon] =image; //writing picture file data into a sandbox[Data writetofile:file Atomically:yes]; }]; }
iOS multi-image download