iOS如何获取网络图片(一)
2016-03-15 22:04
423 查看
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;
//为了避免重复加载的问题,创建了downloadImage,downloadImage属于数据源,当tableview滚动的时候就可以给cell的数据赋值
if (shop.downloadImage) {
//如果下载过,直接从内存中获取图片
cell.iconView.image = shop.downloadImage;
} else {
//设置默认图片
cell.iconView.image = [UIImage imageNamed:@"defaultImage"];
//如果未下载过,开启异步线程
NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock:^{
//模拟网络延时
[NSThread sleepForTimeInterval:1];
//通过url获取网络数据
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",baseUrl,shop.shop_image]]];
//将数据装换为图片
UIImage * image = [UIImage imageWithData:data];
//如果有图片
if (image) {
//通知model,将图片赋值给downloadImage,以便下次从内存获取
shop.downloadImage = image;
//获取主队列,更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//刷新第indexPath单元的表格
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
}
}];
//将请求加入全局队列
[self.queue addOperation:op];
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SXTShopCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
SXTShop * shop = self.dataList[indexPath.row];
cell.shop = shop;
//为了避免重复加载的问题,创建了downloadImage,downloadImage属于数据源,当tableview滚动的时候就可以给cell的数据赋值
if (shop.downloadImage) {
//如果下载过,直接从内存中获取图片
cell.iconView.image = shop.downloadImage;
} else {
//设置默认图片
cell.iconView.image = [UIImage imageNamed:@"defaultImage"];
//如果未下载过,开启异步线程
NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock:^{
//模拟网络延时
[NSThread sleepForTimeInterval:1];
//通过url获取网络数据
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",baseUrl,shop.shop_image]]];
//将数据装换为图片
UIImage * image = [UIImage imageWithData:data];
//如果有图片
if (image) {
//通知model,将图片赋值给downloadImage,以便下次从内存获取
shop.downloadImage = image;
//获取主队列,更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//刷新第indexPath单元的表格
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
}
}];
//将请求加入全局队列
[self.queue addOperation:op];
}
return cell;
}
相关文章推荐
- Android使用Http协议访问网络
- 路由协议RIP、EIGRP、OSPF
- 从零开始写Http框架---第二篇
- springMVC No mapping found for HTTP request with URI
- 使用okhttp报错"Missing type parameter"
- java SE基础(TCP Socket通信)
- http和https的差别
- TCP的流量控制
- python之网络爬虫
- java SE基础(URL访问网络资源)
- HTTP 单线程 模拟迅雷下载
- TCP/IP协议三次握手与四次握手流程解析
- 手写服务器httpserver_准备_Socket入门_htmlJAVA195-196
- TCP和Http的区别!我都搞懂了,你就别迷糊了!
- Okhttp
- android 网络实时监听网络状态变化 及 网络类型判断
- HTTP、TCP、UDP、Socket
- 新博客(http://www.cnblogs.com/shentr/)
- 发布b3log-solo后,访问http://localhost:8080/b3log-solo/提示错误为staticServePath Error。
- [网络流24题][CODEVS1922]骑士共存问题(最大流)