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

一个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. 【jdk源码分析】java.lang.Appendable

    1.概述 public interface Appendable 能够被添加 char 序列和值的对象.如果某个类的实例打算接收取自 Formatter 的格式化输出,那么该类必须实现 Appenda ...

  2. 51nod 1445 变色DNA(最短路变形)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1445 题意: 思路: 挺好的一道题目,如果$colormap[i][j] ...

  3. Vhost.conf 范例

    NameVirtualHost *:80 <VirtualHost *:80> ServerName haofei.com DocumentRoot "E:/test/" ...

  4. [ios][map]自定义地图标注

    参考:http://blog.csdn.net/mad1989/article/details/8794762 ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高 ...

  5. 《剑指offer》第三十二题(不分行从上往下打印二叉树)

    // 面试题32(一):不分行从上往下打印二叉树 // 题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印. #include <iostream> #include ...

  6. Android蓝牙通信功能开发

    1. 概述 Bluetooth 是几乎现在每部手机标准配备的功能,多用于耳机 mic 等设备与手机的连接,除此之外,还可以多部手机之间建立 bluetooth 通信,本文就通过 SDK 中带的一个聊天 ...

  7. Android 上传大文件

    最近工作需要实现使用 Android 手机上传图片的功能, 参考了网络上的很多资料, 不过网络上的代码都仅仅适合上传较小的文件, 当上传较大文件时(我在自己的测试机器上发现是 2M 左右), 就会因为 ...

  8. HttpClient的用法总结

    使用HttpClient连接服务端的步骤: 1.创建HttpClient客户端对象 HttpClient client = new DefaultHttpClient(); 2.创建请求对象      ...

  9. ActivityGroup实现tab功能

    android.app包中含有一个ActivityGroup类,该类是Activity的容器,可以包含多个嵌套进来的 Activitys,这篇文章就是借助ActivityGroup可以嵌套Activi ...

  10. Python requests介绍之接口介绍

    Python requests介绍 引用官网介绍 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. Requests 允许你发送纯天然,植物饲养的 HTTP/1. ...