多线程之多图下载
一、缓存图片策略:操作顺序:内存->缓存->下载->写入缓存
1 2 3 4 5 | 1.首先从内存中查看有没有下载该图片->如果有展示到cell上。 2.如果内存中没有->查看沙盒中有没有该图片->如果有从沙盒中获取图片->展示到cell上。 3.如果缓存中没有->显示占位图->查看该任务是否在下载->如果下载,不做任务操作,继续下载。 4.如果没该任务没有下载->创建下载操作,并存取个标记->下载完毕后,移除该下载任务,并存到内存数组中->刷新表格->图片写入沙盒。 5.并内存警告时,移除所有的下载操作,并清空内存数组。 |
二、事例代码:
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 72 73 74 | //先去查看内存缓存中该图片时候已经存在,如果存在那么直接显示到cell,否则去检查磁盘缓存 //如果有磁盘缓存,那么保存一份到内存,设置图片,否则就直接下载 //1)没有下载过 //2)重新打开程序 //images字典是内存通过存取图片地址来查看该图片是否存在,防止图片重复下载 UIImage *image = [self.images objectForKey:appM.icon]; if (image) { cell.imageView.image = image; NSLog( @"%zd处的图片使用了内存缓存中的图片" ,indexPath.row) ; } else { //如果内存中没有该图片 //保存图片到沙盒缓存 NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; //获得图片的名称,不能包含/,如http://imga/sms/12346.png,只取1245.png. NSString *fileName = [appM.icon lastPathComponent]; //拼接图片的全路径 NSString *fullPath = [caches stringByAppendingPathComponent:fileName]; //检查磁盘缓存 NSData *imageData = [NSData dataWithContentsOfFile:fullPath]; //初始化设置为空 imageData = nil; if (imageData) { //如果缓存中存在,读取缓存图片,设置到cell上 UIImage *image = [UIImage imageWithData:imageData]; cell.imageView.image = image; NSLog( @"%zd处的图片使用了磁盘缓存中的图片" ,indexPath.row) ; //把图片保存到内存缓存 [self.images setObject:image forKey:appM.icon]; // NSLog(@"%@",fullPath); } else { //如果缓存中没有该图片 //查看该图片是否正在下载或者说任务中是否有该任务 NSBlockOperation *download = [self.operations objectForKey:appM.icon]; if (download) { //如果正在下载,什么也不操作 } else { //先清空cell原来的图片,因为涉及到复用所以每次设置好占位图 cell.imageView.image = [UIImage imageNamed: @"Snip20160221_306" ]; download = [NSBlockOperation blockOperationWithBlock:^{ //开始下载 NSURL *url = [NSURL URLWithString:appM.icon]; NSData *imageData = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:imageData]; NSLog( @"%zd--下载---" ,indexPath.row); //容错处理 if (image == nil) { //如果图片地址或其它原因造成图片为空,清空了本次下载,之后可以重新下载 [self.operations removeObjectForKey:appM.icon]; return ; } //演示网速慢的情况 //[NSThread sleepForTimeInterval:3.0]; //把图片保存到内存缓存 [self.images setObject:image forKey:appM.icon]; //NSLog(@"Download---%@",[NSThread currentThread]); //线程间通信,回到主线程刷新UI [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //cell.imageView.image = image; //刷新一行 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; //NSLog(@"UI---%@",[NSThread currentThread]); }]; //写数据到沙盒 [imageData writeToFile:fullPath atomically:YES]; //移除图片的下载操作 [self.operations removeObjectForKey:appM.icon]; }]; //添加操作到操作缓存中 [self.operations setObject:download forKey:appM.icon]; //添加操作到队列中 [self.queue addOperation:download]; } } } |
内存警告时处理
1 2 3 4 5 6 | -( void )didReceiveMemoryWarning { [self.images removeAllObjects]; //取消队列中所有的操作 [self.queue cancelAllOperations]; } |
三、第三方实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* 第一个参数:下载图片的url地址 第二个参数:占位图片 第三个参数:progress 进度回调 receivedSize:已经下载的数据大小 expectedSize:要下载图片的总大小 第四个参数: image:要下载的图片 error:错误信息 cacheType:缓存类型 imageURL:图片url */ [cell.imageView sd_setImageWithURL:[NSURL URLWithString:appM.icon] placeholderImage:[UIImage imageNamed: @"Snip20160221_306" ] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog( @"%f" ,1.0 * receivedSize / expectedSize); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { NSLog( @"%zd" ,cacheType); }]; |
将来的自己,会感谢现在不放弃的自己!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
2016-04-17 消息转发机制入门篇