写在前面
在ios开发中,无论是在uitableview还是在uicollectionview中,通过网络获取图片设置到cell上是较为常见的需求。尽管有很多现存的第三方库可以将下载和缓存功能都封装好了供开发者使用,但从学习的角度出发,看懂源码,理解其中的原理,结合自身的实际需求写出自己的代码是很必要的。在刚结束的demo中,有用到异步图片下载功能,这篇笔记就是对整个实现的简单整理。
基本思路
•cell中添加一个uiimageview
•cell拥有url,发起下载请求,注册下次完成通告,在通告处理时间中获取下载图片并设置
•下载管理类负责开启下载线程和各种缓存(内存+文件),下载完成后发送下载完成通告
•为避免cell重用和异步下载造成的图片错位,cell在发起下载前为自身imageview设置默认图片,同时为imageview设置tag
整体框架
关键代码
cell初始化,并注册下载完成通告
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@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通知处理事件
?
1
2
3
4
5
6
7
8
9
10
11
|
//通知处理事件
- ( 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发起下载请求
?
1
2
3
4
5
6
7
8
9
10
11
12
|
- ( 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];
}
|
下载管理类下载函数
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
- ( 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];
}
|
控制器中使用
?
1
2
3
4
5
6
7
8
9
|
- (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的,虽然可以直接看到源码,也有一些文章和博客讲解思路,但自己在没有接触过多线程编程的情况下学习这个下载思路还是花了挺多时间的。前期一直都有些着急,想要赶紧做出来,在对好多东西都是懵懵懂懂的情况下就去做了,后来才慢慢意识到,其实慢就是快,慢下来,把问题想清楚了再去实施虽然前期感觉是不太好的,但到越到后面就越能发现这种慢的好处。
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 72
-
2025-05-29 29
-
2025-05-25 19
-
2025-05-25 31
-
2025-05-25 79
热门评论