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