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

一个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. confluence导出pdf 文字显示不全

    当使用confluence编辑页面时,当一行的文字过多,且中间没什么逗号分隔时,有时会出现导出的pdf文件中,这一行显示的文字不全的情况. 如: 很明显费用的费字没有显示完全,且后面还有其他的字. 可 ...

  2. AngularJS监听路由变化

    使用AngularJS时,当路由发生改变时,我们需要做某些处理,此时可以监听路由事件,常用的是$routeStartChange, $routeChangeSuccess.完整例子如下: <!D ...

  3. python flask demo

    from flask import Flask, jsonify from flask import abort from flask import make_response from flask ...

  4. Qt4_WebKit_例子

    1. 安装包:qt-opensource-windows-x86-vs2010-4.8.6.exe Qt4已经编译好的文件:E:\ZC_software_installDir\Qt_4.8.6\dem ...

  5. Linux环境下 RabbitMQ 的下载与安装

    0 环境 CentOS7 RabbitMQ 3.6.5 erlang 18.3 socat rabbitmq是使用erlang语言编写的,所以需要先安装erlang,其次rabbitmq安装依赖于so ...

  6. 雷林鹏分享:C# 委托(Delegate)

    C# 委托(Delegate) C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针.委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托 ...

  7. [.NET开发] C#编程调用Cards.dll实现图形化发牌功能示例

    本文实例讲述了C#编程调用Cards.dll实现图形化发牌功能.分享给大家供大家参考,具体如下: using System; using System.Collections.Generic; usi ...

  8. WPF中的动画

    动画无疑是WPF中最吸引人的特色之一,其可以像Flash一样平滑地播放并与程序逻辑进行很好的交互.这里我们讨论一下故事板. 在WPF中我们采用Storyboard(故事板)的方式来编写动画,为了对St ...

  9. English trip WeekEnd-Lesson 2018.11.10

    本周末上了三节课,做个小结吧\(^o^)/~: [102] 新概念一早读 - 27 - 28        Teacher: March Mrs. Smith's living room is lar ...

  10. Sereja and Table CodeForces - 425B (暴力,状压)

    大意: 给定01矩阵, 求翻转尽量少的数字, 使得所有0或1的连通块为矩形, 若至少要翻转超过k次, 输出-1