Static NSString * BaseUrl = @ "http://192.168.1.123/images/";
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {
Sxtshopcell * cell = [TableView dequeuereusablecellwithidentifier:identifier];
Sxtshop * shop = Self.datalist[indexpath.row];
Cell.shop = shop;
In order to avoid the problem of repeated loading, the creation of Downloadimage,downloadimage belongs to the data source, and when TableView scrolls, it can assign values to the cell's data.
if (shop.downloadimage) {
If downloaded, get a picture directly from memory
Cell.iconView.image = Shop.downloadimage;
} else {
Set default picture
Cell.iconView.image = [UIImage imagenamed:@ "Defaultimage"];
Open an async thread if it has not been downloaded
Nsblockoperation * op = [nsblockoperation blockoperationwithblock:^{
Analog Network delay
[Nsthread sleepfortimeinterval:1];
Get network data by URL
NSData * data = [NSData datawithcontentsofurl:[nsurl urlwithstring:[nsstring stringwithformat:@ "%@%@", BASEURL, Shop.shop_image]];
Reload data as a picture
UIImage * image = [UIImage imagewithdata:data];
If you have a picture
if (image) {
Notifies model, assigns a picture to downloadimage, so that the next time it gets from memory
Shop.downloadimage = image;
Get the Home column, update the UI
[[Nsoperationqueue Mainqueue] addoperationwithblock:^{
Refresh the table for unit Indexpath
[Self.tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationnone];
}];
}
}];
Join a request to a global queue
[Self.queue Addoperation:op];
}
return cell;
}
How iOS gets network pictures (i)