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

一个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. 线程面试top50题

    转载:java线程面试题: 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员的欢迎.大多数待遇丰厚的Java开 ...

  2. Change the Forwarding: RMT Architecture

    Change the Forwarding: RMT Architecture Note on "Forwarding Metamorphosis: Fast Programmable Ma ...

  3. Jmeter 分布式压力测试

      JMeter中进行分布式测试 作为一个纯 JAVA 的GUI应用,JMeter对于CPU和内存的消耗还是很惊人的,所以当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心, ...

  4. Qt5.3.2_CentOS6.4(x86)_代码文件编码

    1.1.1.Qt5.3.2_MinGW 在Windows中安装时,默认的文件编码是 UTF8. 1.2.在 CentOS6.4中安装 qt-opensource-linux-x86-5.3.2.run ...

  5. [ios]object-c math.h里的数学计算公式介绍

    参考:http://blog.csdn.net/yuhuangc/article/details/7639117 头文件:<math.h> 1. 三角函数  double sin (dou ...

  6. 《剑指offer》第三十五题(复杂链表的复制)

    // 面试题35:复杂链表的复制 // 题目:请实现函数ComplexListNode* Clone(ComplexListNode* pHead),复 // 制一个复杂链表.在复杂链表中,每个结点除 ...

  7. 《剑指offer》第二十八题(对称的二叉树)

    // 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...

  8. 修改unity变量名但不丢失序列化值

    using UnityEngine; using UnityEngine.Serialization; public class LgsTest : MonoBehaviour { [Formerly ...

  9. 动态规划3--Help Jimmy

    动态规划3--Help Jimmy 一.心得 二.题目 三.分析 Jimmy跳到一块板上后,可以有两种选择,向左走,或向右走.走到左端和走到右端所需的时间,是很容易算的.如果我们能知道,以左端为起点到 ...

  10. English trip V1 - 8.What's in My Bag? 我的包里面有什么? Teacher:Corrine Key: plular(复数) and singular(单数)

    In this lesson you will learn to talk about the things you have.   您将学习如何谈论您拥有的东西 课上内容(Lesson) What' ...