iOS多线程实现多图下载功能
本文实例为大家分享了iOS多线程实现多图下载功能的具体代码,供大家参考,具体内容如下
一.模型文件代码如下
//XMGAPP.h #import@interfaceXMGAPP:NSObject /**APP的名称*/ @property(nonatomic,strong)NSString*name; /**APP的图片的url地址*/ @property(nonatomic,strong)NSString*icon; /**APP的下载量*/ @property(nonatomic,strong)NSString*download; +(instancetype)appWithDict:(NSDictionary*)dict; @end
//XMGAPP.m
#import"XMGAPP.h"
@implementationXMGAPP
+(instancetype)appWithDict:(NSDictionary*)dict
{
XMGAPP*appM=[[XMGAPPalloc]init];
//KVC
[appMsetValuesForKeysWithDictionary:dict];
returnappM;
}
@end
控制器.m代码如下:
//ViewController.m
#import"ViewController.h"
#import"XMGAPP.h"
@interfaceViewController()
/**tableView的数据源*/
@property(nonatomic,strong)NSArray*apps;
/**内存缓存*/
@property(nonatomic,strong)NSMutableDictionary*images;
/**队列*/
@property(nonatomic,strong)NSOperationQueue*queue;
/**操作缓存*/
@property(nonatomic,strong)NSMutableDictionary*operations;
@end
@implementationViewController
#pragmamark----------------------
#pragmamarklazyloading
-(NSOperationQueue*)queue
{
if(_queue==nil){
_queue=[[NSOperationQueuealloc]init];
//设置最大并发数
_queue.maxConcurrentOperationCount=5;
}
return_queue;
}
-(NSMutableDictionary*)images
{
if(_images==nil){
_images=[NSMutableDictionarydictionary];
}
return_images;
}
-(NSArray*)apps
{
if(_apps==nil){
//字典数组
NSArray*arrayM=[NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"apps.plist"ofType:nil]];
//字典数组---->模型数组
NSMutableArray*arrM=[NSMutableArrayarray];
for(NSDictionary*dictinarrayM){
[arrMaddObject:[XMGAPPappWithDict:dict]];
}
_apps=arrM;
}
return_apps;
}
-(NSMutableDictionary*)operations
{
if(_operations==nil){
_operations=[NSMutableDictionarydictionary];
}
return_operations;
}
#pragmamark----------------------
#pragmamarkUITableViewDatasource
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return1;
}
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section
{
returnself.apps.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*ID=@"app";
//1.创建cell
UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:ID];
//2.设置cell的数据
//2.1拿到该行cell对应的数据
XMGAPP*appM=self.apps[indexPath.row];
//2.2设置标题
cell.textLabel.text=appM.name;
//2.3设置子标题
cell.detailTextLabel.text=appM.download;
//2.4设置图标
//先去查看内存缓存中该图片时候已经存在,如果存在那么久直接拿来用,否则去检查磁盘缓存
//如果有磁盘缓存,那么保存一份到内存,设置图片,否则就直接下载
//1)没有下载过
//2)重新打开程序
UIImage*image=[self.imagesobjectForKey:appM.icon];
if(image){
cell.imageView.image=image;
NSLog(@"%zd处的图片使用了内存缓存中的图片",indexPath.row);
}else
{
//保存图片到沙盒缓存
NSString*caches=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject];
//获得图片的名称,不能包含/
NSString*fileName=[appM.iconlastPathComponent];
//拼接图片的全路径
NSString*fullPath=[cachesstringByAppendingPathComponent:fileName];
//检查磁盘缓存
NSData*imageData=[NSDatadataWithContentsOfFile:fullPath];
//废除
imageData=nil;
if(imageData){
UIImage*image=[UIImageimageWithData:imageData];
cell.imageView.image=image;
NSLog(@"%zd处的图片使用了磁盘缓存中的图片",indexPath.row);
//把图片保存到内存缓存
[self.imagessetObject:imageforKey:appM.icon];
//NSLog(@"%@",fullPath);
}else
{
//检查该图片时候正在下载,如果是那么久什么都捕捉,否则再添加下载任务
NSBlockOperation*download=[self.operationsobjectForKey:appM.icon];
if(download){
}else
{
//先清空cell原来的图片
cell.imageView.image=[UIImageimageNamed:@"Snip20160221_306"];
download=[NSBlockOperationblockOperationWithBlock:^{
NSURL*url=[NSURLURLWithString:appM.icon];
NSData*imageData=[NSDatadataWithContentsOfURL:url];
UIImage*image=[UIImageimageWithData:imageData];
NSLog(@"%zd--下载---",indexPath.row);
//容错处理
if(image==nil){
[self.operationsremoveObjectForKey:appM.icon];
return;
}
//演示网速慢的情况
//[NSThreadsleepForTimeInterval:3.0];
//把图片保存到内存缓存
[self.imagessetObject:imageforKey:appM.icon];
//NSLog(@"Download---%@",[NSThreadcurrentThread]);
//线程间通信
[[NSOperationQueuemainQueue]addOperationWithBlock:^{
//cell.imageView.image=image;
//刷新一行
[self.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
//NSLog(@"UI---%@",[NSThreadcurrentThread]);
}];
//写数据到沙盒
[imageDatawriteToFile:fullPathatomically:YES];
//移除图片的下载操作
[self.operationsremoveObjectForKey:appM.icon];
}];
//添加操作到操作缓存中
[self.operationssetObject:downloadforKey:appM.icon];
//添加操作到队列中
[self.queueaddOperation:download];
}
}
}
//3.返回cell
returncell;
}
-(void)didReceiveMemoryWarning
{
[self.imagesremoveAllObjects];
//取消队列中所有的操作
[self.queuecancelAllOperations];
}
//1.UI很不流畅--->开子线程下载图片
//2.图片重复下载--->先把之前已经下载的图片保存起来(字典)
//内存缓存--->磁盘缓存
//3.图片不会刷新--->刷新某行
//4.图片重复下载(图片下载需要时间,当图片还未完全下载之前,又要重新显示该图片)
//5.数据错乱---设置占位图片
/*
Documents:会备份,不允许
Libray
Preferences:偏好设置保存账号
caches:缓存文件
tmp:临时路径(随时会被删除)
*/
@end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。