多线程之多图下载

一、缓存图片策略:操作顺序:内存->缓存->下载->写入缓存
1.首先从内存中查看有没有下载该图片->如果有展示到cell上。 2.如果内存中没有->查看沙盒中有没有该图片->如果有从沙盒中获取图片->展示到cell上。 3.如果缓存中没有->显示占位图->查看该任务是否在下载->如果下载,不做任务操作,继续下载。 4.如果没该任务没有下载->创建下载操作,并存取个标记->下载完毕后,移除该下载任务,并存到内存数组中->刷新表格->图片写入沙盒。 5.并内存警告时,移除所有的下载操作,并清空内存数组。
二、事例代码:
//先去查看内存缓存中该图片时候已经存在,如果存在那么直接显示到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];
}
}
}
内存警告时处理
-(void)didReceiveMemoryWarning
{
[self.images removeAllObjects];
//取消队列中所有的操作
[self.queue cancelAllOperations];
}
三、第三方实现
/*
第一个参数:下载图片的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);
}];
将来的自己,会感谢现在不放弃的自己!

浙公网安备 33010602011771号