重网上下载图片是很慢的,为了不影响体验,选择延时加载图片是很好的办法。

一个tableView 列表,左边暂时没有图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

static NSString *CellIdentifier = @"myCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)

{

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle

reuseIdentifier:CellIdentifier] autorelease];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

// 设置cell一些数据

AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];

cell.textLabel.text = appRecord.appName;

cell.detailTextLabel.text = appRecord.artist;

// 如果不存在图片

if (!appRecord.appIcon)

{

if (self.tableView.dragging == NO && self.tableView.decelerating == NO)//不在拖动中和减速时,开始下载图片

{

[self startIconDownload:appRecord forIndexPath:indexPath];

}

//设置图片为空白图片(等待下载)

cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];

}

//如果有图片

else

{

cell.imageView.image = appRecord.appIcon;

}

return cell;

}

 
关键就是[self startIconDownload:appRecord forIndexPath:indexPath];

- (void)startIconDownload:(AppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath

{

IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

if (iconDownloader == nil) //已经在下载中的不用重复下载了,没有在下载中就往下走

{

iconDownloader = [[IconDownloader alloc] init];

iconDownloader.appRecord = appRecord;

iconDownloader.indexPathInTableView = indexPath;

iconDownloader.delegate = self;

[imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];

[iconDownloader startDownload];

[iconDownloader release];

}

}

IconDownloader 是一个下载图片封装类
关键方法:iconDownloader.delegate = self;
[iconDownloader startDownload];
 
一个是委托,将来告诉self下载完成更新图片
一个是自己方法开始联网下载图片
 
委托调用方法,重设图片

- (void)appImageDidLoad:(NSIndexPath *)indexPath

{

IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath];

if (iconDownloader != nil)

{

UITableViewCell *cell = [self.tableViewcellForRowAtIndexPath:iconDownloader.indexPathInTableView];

cell.imageView.image = iconDownloader.appRecord.appIcon;

}

}

 
类IconDownloader 中的方法

- (void)startDownload

{

self.activeDownload = [NSMutableData data];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:

[NSURLRequest requestWithURL:

[NSURL URLWithString:appRecord.imageURLString]] delegate:self];

self.imageConnection = conn;

[conn release];

}

最后 NSURLConnection的委托需要自己实现了。

IOS延时加载网络图片的更多相关文章

  1. iOS 延时加载

    这里列举了四种线程延时加载的方法, 1.performSelector方法 此方法必须在主线程中执行,并不是阻塞当前的线程 [self performSelector:@selector(delayM ...

  2. IOS UIwebView 加载网络图片 使用相对地址

    方法一: 在html文件内直接使用file:///user//xx//image.png的绝对路径 注:这样可以显示图片,但是如果在程序目录修改,图片就不能显示 方法二: 在html使用占位符,如:在 ...

  3. ios UIImageView异步加载网络图片

    方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...

  4. iOS图片加载框架-SDWebImage解读

    在iOS的图片加载框架中,SDWebImage可谓是占据大半壁江山.它支持从网络中下载且缓存图片,并设置图片到对应的UIImageView控件或者UIButton控件.在项目中使用SDWebImage ...

  5. UIImageView异步加载网络图片

    在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: 去下载https://github.com/rs/SDWebImage放进你的工程里,加入头文件# ...

  6. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化   在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...

  7. iOS 图片加载框架- SDWebImage 解读

    在iOS的图片加载框架中,SDWebImage可谓是占据大半壁江山.它支持从网络中下载且缓存图片,并设置图片到对应的UIImageView控件或者UIButton控件.在项目中使用SDWebImage ...

  8. Ionic 图片延时加载

    图片的延时加载是为了提供App的运行效率,那么是如何实现的呢?献上github:  https://github.com/paveisistemas/ionic-image-lazy-load 1.下 ...

  9. iOS网络加载图片缓存与SDWebImage

    加载网络图片可以说是网络应用中必备的.如果单纯的去下载图片,而不去做多线程.缓存等技术去优化,加载图片时的效果与用户体验就会很差. 一.自己实现加载图片的方法 tips: *iOS中所有网络访问都是异 ...

随机推荐

  1. [Pytorch]Pytorch中tensor常用语法

    原文地址:https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别. 这次我把常用的Tensor的数学运算总结到 ...

  2. bzoj 4403 序列统计 卢卡斯定理

    4403:序列统计 Time Limit: 3 Sec  Memory Limit: 128 MB Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调 ...

  3. ubuntu server 16.04(amd 64) 配置网桥,多网卡使用激活

    安装了Ubuntu16.04的server版本,结果进入系统输入ifconfig后发现,只有一个网卡enp1s0,还有一个网络回路lo,ifconfig -a 发现其实一共有四个网卡,enp1s0,e ...

  4. python排序(插入排序) 从小到大顺序

    def insert_sort(ilist): for i in range(len(ilist)): for j in range(i): if ilist[i] < ilist[j]: il ...

  5. .NET身份证验证

    身份证号码编码规则及校验位校验算法 算法地址:http://jingyan.baidu.com/article/7f41ececff944a593d095c8c.html 简单验证长度 /// < ...

  6. kali删除软件

    kali中主要为2种卸载方法:1.apt2.dpkg 使用apt的方式有:apt-get remove [package]apt-get remove --purge # ------(package ...

  7. 使用 XPath 选择器

    在前面的内容中,我们掌握了一些 CSS 选择器和它们的使用方法,以及 rvest 包中用于提取网页内容的函数.一般来说,CSS 选择器足够满足绝大部分的 HTML 节点匹配的需要.但是,当需要根据某些 ...

  8. VC_可再发行组件包

    1. 中文 : 可再发行组件包 英文 : Redistributable Package 例子 : Download Microsoft Visual C++ 2010 Redistributable ...

  9. Idea设置默认不折叠一行的函数

  10. Pyhon 日志管理 -- logging

    Pyhon 日志管理 -- logging 一直觉得运行程序是能打印日志是一个神奇的事情,不懂日志产生的原理,后来听说Pyhton 有一个logging模块,So,要好好研究一下. 先贴出代码,看看她 ...