How iOS gets network pictures (i)

Source: Internet
Author: User

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)

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.