当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS异步下载图片实例代码

iOS异步下载图片实例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

东高地一中,爱情公寓1迅雷下载,笔记本能换显卡么

写在前面

在ios开发中,无论是在uitableview还是在uicollectionview中,通过网络获取图片设置到cell上是较为常见的需求。尽管有很多现存的第三方库可以将下载和缓存功能都封装好了供开发者使用,但从学习的角度出发,看懂源码,理解其中的原理,结合自身的实际需求写出自己的代码是很必要的。在刚结束的demo中,有用到异步图片下载功能,这篇笔记就是对整个实现的简单整理。

基本思路

•cell中添加一个uiimageview

•cell拥有url,发起下载请求,注册下次完成通告,在通告处理时间中获取下载图片并设置

•下载管理类负责开启下载线程和各种缓存(内存+文件),下载完成后发送下载完成通告

•为避免cell重用和异步下载造成的图片错位,cell在发起下载前为自身imageview设置默认图片,同时为imageview设置tag

整体框架

 

关键代码

cell初始化,并注册下载完成通告

@interface sqphotocell ()
@property (strong, nonatomic) uiimageview *photoview;
//tag指向当前可见图片的url,可过滤掉已经滑出屏幕的图片的url
@property (strong, nonatomic) nsstring *imageviewtag;
@end
-(id)initwithframe:(cgrect)frame
{
self = [super initwithframe:frame];
if (self)
{
_photoview = [[uiimageview alloc] initwithframe:cgrectzero];
_photoview.userinteractionenabled = yes;
[self.contentview addsubview:_photoview];
_imageviewtag = @"";
//注册下载完成通知
[[nsnotificationcenter defaultcenter] addobserver:self
selector:@selector(downloadcallback:)
name:notification_download_callback
object:nil];
}
return self;
}

cell通知处理事件

//通知处理事件
- (void)downloadcallback:(nsnotification *)noti
{
nsdictionary *notidic = noti.userinfo;
nsstring *urlstr = [notidic objectforkey:@"urlstr"];
uiimage *image = [notidic objectforkey:@"image"];
if ([self.imageviewtag isequaltostring:urlstr])
{
self.photoview.image = image;
}
}

cell发起下载请求

- (void)setimagewithurl:(nsstring *)urlstr placeholder:(uiimage *)placeholder
{
self.imageviewtag = urlstr;
//预设图片,用于清除复用以前可能存在的图片
self.photoview.image = placeholder;
if (urlstr)
{
sqwebimagemanager *manager = [sqwebimagemanager sharedmanager];
[manager downloadimagewithurlstring:urlstr];
}
[self setneedsdisplay];
}

下载管理类下载函数

- (void)downloadimagewithurlstring:(nsstring *)urlstr
{
// 1.判断内存缓存 {urlstr: image}
uiimage *cacheimage = [self.imagecache objectforkey:urlstr];
if (cacheimage != nil)
{
//发出下载完成的通知,并传回urlstr和图片
[self postdownloadcompletenotification:urlstr withimage:cacheimage];
return;
}
// 2.判断沙盒缓存
nsstring *cacheimagepath = [self cacheimagepathwithurlstring:urlstr];
cacheimage = [uiimage imagewithcontentsoffile:cacheimagepath];
if (cacheimage != nil)
{
// 从沙盒中读取到了图片,设置到内存缓存中,方便下次可以直接从内存中读取
[self.imagecache setobject:cacheimage forkey:urlstr];
// 返回图片
[self postdownloadcompletenotification:urlstr withimage:cacheimage];
return;
}
// 3.判断操作缓存,防止图片多次下载 {urlstr: operation}
if (self.operationcache[urlstr] != nil)
{
// 有操作正在下载这张图片
nslog(@"有操作正在下载这张图片");
return;
}
// 1.定义下载图片操作
sqdownloadoperation *downloadoperation = [sqdownloadoperation downloadoperationwithurlstring:urlstr cacheimagepath:cacheimagepath];
// 设置操作下载完成的回调,当 downloadoperation 的 main 方法执行完成的时候回调用
__weak typeof(downloadoperation) weakdownloadoperation = downloadoperation;
downloadoperation.completionblock = ^() {
// 1. 获取下载完成的图像
uiimage *image = [weakdownloadoperation getdownloadimage];
// 2. 从操作缓冲池中删除操作
[self.operationcache removeobjectforkey:urlstr];
// 3. 判断图像是否为空(缩略图)
if (image != nil)
{
// 设置下载的图片到图片内存缓存中
[self.imagecache setobject:image forkey:urlstr];
// 4. 主线程回调
[[nsoperationqueue mainqueue] addoperationwithblock:^{
//发出下载完成通告
[self postdownloadcompletenotification:urlstr withimage:image];
}];
}
else
{
//如果图片为空,返回下载失败时的默认图片
image = [uiimage imagenamed:@"default.jpg"];
// 4. 主线程回调
[[nsoperationqueue mainqueue] addoperationwithblock:^{
//发出下载完成通告
[self postdownloadcompletenotification:urlstr withimage:image];
}];
}
};
// 2.将下载图片操作添加到队列中
[self.downloadqueue addoperation:downloadoperation];
// 3.将下载图片操作添加到下载操作缓存中
[self.operationcache setobject:downloadoperation forkey:urlstr];
}
- (void)postdownloadcompletenotification:(nsstring *)urlstr withimage:(uiimage *)image
{
nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:urlstr, @"urlstr", image, @"image",nil];
[[nsnotificationcenter defaultcenter]postnotificationname:notification_download_callback
object:nil
userinfo:dic];
}

控制器中使用

- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath
{
sqphotocell *cell = (sqphotocell *)[collectionview dequeuereusablecellwithreuseidentifier:reuseidentifier
forindexpath:indexpath];
uiimage *placeholder = [uiimage imagenamed:@"gray.jpg"];
nsstring *imageurl = @"http://www.taopic.com/uploads/allimg/110925/9117-11092509545328.jpg";
[cell setimagewithurl:imageurl placeholder:placeholder];
return cell;
}

写在后面

这个异步下载图片的思路是仿照sdwebimage的,虽然可以直接看到源码,也有一些文章和博客讲解思路,但自己在没有接触过多线程编程的情况下学习这个下载思路还是花了挺多时间的。前期一直都有些着急,想要赶紧做出来,在对好多东西都是懵懵懂懂的情况下就去做了,后来才慢慢意识到,其实慢就是快,慢下来,把问题想清楚了再去实施虽然前期感觉是不太好的,但到越到后面就越能发现这种慢的好处。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网