TableView Picture Cache

Source: Internet
Author: User

You need to cache the picture into the sandbox, use multi-threaded download diagram to put the queue into the dictionary, the value thread key to download the path to determine if there is no key created to add a thread to download

Assign a picture just refresh the corresponding cell to

@property (Nonatomic,strong) nsmutabledictionary * Imagecache;
@property (Nonatomic,strong) nsmutabledictionary * operations;
@property (nonatomic,strong) nsoperationqueue * queue;

-(Nsoperationqueue *) queue
{
if (!_queue) {
_queue=[[nsoperationqueue Alloc]init];
}
return _queue;
}
-(Nsmutabledictionary *) operations
{
if (!_operations) {
_operations=[nsmutabledictionary dictionary];
}
return _operations;
}
-(Nsmutabledictionary *) imagecache{
if (!_imagecache) {
_imagecache=[nsmutabledictionary dictionary];
}
return _imagecache;
}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{
Cartableviewcell *cell=[[cartableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseIdentifier:@ "int "];


static nsstring* [email protected] "app";
Cartableviewcell * Cell=[tableview Dequeuereusablecellwithidentifier:id];

if (!cell) {
Cell=[[cartableviewcell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:id];
}


if (alldataarray.count>0) {
Nsdictionary * Unit=[alldataarray ObjectAtIndex:indexPath.row];
Cell.titlename.text=[nsstring stringwithformat:@ "%@%@", [Unit objectforkey:@ "Carcname"],[unit objectForKey:@ " Carkname "];
Cell.timeanddistance.text=[nsstring stringwithformat:@ "%@/%@ km", [unit objectforkey:@ "Inspectiondate"],[unit objectforkey:@ "mileage"];
Cell.money.text=[nsstring stringwithformat:@ "%@ million", [Unit objectforkey:@ "Price"];
NSString *imagearr=[unit objectforkey:@ "img"];
Nsarray * Arr=[imagearr componentsseparatedbystring:@ ";"];


NSString * path=[nsstring stringwithformat:@ "http://121.201.18.34:8080/car/upload/car/%@", Arr[0]];

UIImage * Image=self.imagecache[path];
if (image) {
Cell. Carimageview.image=image;
}else {

NSString * Cachespath=[nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) firstObject];
NSString * Filename=[path lastpathcomponent];
Calculate the full path of a file
NSString * file =[cachespath stringbyappendingpathcomponent:filename];
Loading the sandbox's file data
NSData * Data=[nsdata Datawithcontentsoffile:file];
NSLog (@ "%@", file);
if (data) {
UIImage * Image=[uiimage Imagewithdata:data];
Cell. Carimageview.image=image;

}else{
Nsoperation * operation = Self.operations[path];
if (Operation==nil) {
Operation=[nsblockoperation blockoperationwithblock:^{
Download image
NSData * Data=[nsdata Datawithcontentsofurl:[nsurl Urlwithstring:path];

Data Load Failure
if (Data==nil) {
Removal action
[Self.operations Removeobjectforkey:path];
return;
}


UIImage * Image=[uiimage Imagewithdata:data];
Back to main thread display picture
[[Nsoperationqueue mainqueue]addoperationwithblock:^{
[TableView Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationnone];
}];
Store in a dictionary
Self.imagecache[path]=image;
Write a picture in a sandbox
[Data writetofile:file Atomically:yes];

}];
Self.operations[path]=operation;
[Self.queue addoperation:operation];
}
}


}

[Cell. Carimageview sd_setimagewithurl:[nsurl urlwithstring:[nsstring stringwithformat:@ "http://121.201.18.34:8080/CAR/ upload/car/%@ ", Arr[0]]];
}
return cell;
}

TableView Picture Cache

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.