利用多線程進行優(yōu)化
#import"ViewController.h"
#import"App.h"
@interfaceViewController()
@property(nonatomic,strong)NSArray*apps;
//內存緩存
@property(nonatomic,strong)NSMutableDictionary*imageDic;
@property(nonatomic,strong)NSOperationQueue*queue;
@property(nonatomic,strong)NSMutableDictionary*operation;
@end
@implementationViewController
- (NSMutableDictionary*)operation{
if(_operation==nil) {
_operation= [NSMutableDictionarydictionary];
}
return_operation;
}
- (NSOperationQueue*)queue{
if(_queue==nil) {
_queue= [[NSOperationQueuealloc]init];
//設置最大并發(fā)數(shù)
_queue.maxConcurrentOperationCount=5;
}
return_queue;
}
- (NSMutableDictionary*)imageDic{
if(_imageDic==nil) {
_imageDic= [NSMutableDictionarydictionary];
}
return_imageDic;
}
- (NSArray*)apps{
if(_apps==nil) {
NSArray*ary = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"apps"ofType:@"plist"]];
NSMutableArray*ary1 = [NSMutableArrayarray];
for(NSDictionary*dicinary) {
[ary1addObject:[AppappWithdic:dic]];
}
_apps= ary1;
}
return_apps;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
returnself.apps.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return1;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
staticNSString* ID =@"app";
UITableViewCell*cell =[tableViewdequeueReusableCellWithIdentifier:ID];
App*app =_apps[indexPath.row];
cell.textLabel.text= app.name;
cell.detailTextLabel.text=app.download;
//下載圖片
//先查看圖片在內存緩存中是否存在久橙,如果存在儒鹿,直接拿來用潮瓶,但程序重新啟動時浮庐,還要重新下載
//如果有磁盤緩存把磁盤緩存放到內存緩存,否則直接下載
//1.沒有下載過
//2.下載過被銷毀了
UIImage*imaged = [self.imageDicobjectForKey:app.icon];
if(imaged) {
cell.imageView.image=imaged;
NSLog(@"%ld---內存緩存",(long)indexPath.row);
}else{
NSString*Caches =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES).lastObject;
NSString*filename = [app.iconlastPathComponent];
NSString* fullpath = [CachesstringByAppendingPathComponent:filename];
//檢查磁盤緩存:
NSData*imageD = [NSDatadataWithContentsOfFile:fullpath];
if(imageD) {
UIImage*image = [UIImageimageWithData:imageD];
cell.imageView.image=image;
NSLog(@"%ld---磁盤緩存",(long)indexPath.row);
//把磁盤緩存放到內存緩存
[self.imageDicsetObject:imageforKey:app.icon];
}else{
//檢查該操作是否在緩存中霜威,如果是就什么也不做
NSBlockOperation*download = [self.operationobjectForKey:app.icon];
if(download) {
}else{
//先清空image,放個占位的圖片
cell.imageView.image=[UIImageimageNamed:@"Snip20170530_1"];
download = [NSBlockOperationblockOperationWithBlock:^{
NSURL*url = [NSURLURLWithString:app.icon];
NSData*imageData = [NSDatadataWithContentsOfURL:url];
UIImage*image = [UIImageimageWithData:imageData];
//容錯處理比藻,防止url不正確绿语,或者網(wǎng)絡問題
if(image ==nil){
[self.operationremoveObjectForKey:app.icon];
return;
}
//演示網(wǎng)速慢的情況
[NSThreadsleepForTimeInterval:3.0];
NSLog(@"dowunload--%@",[NSThreadcurrentThread]);
//線程間通信
[[NSOperationQueuemainQueue]addOperationWithBlock:^{
//刷新某一行
[self.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
cell.imageView.image=image;
}];
//把圖片保存在內存緩存里
[self.imageDicsetObject:imageforKey:app.icon];
[imageDatawriteToFile:fullpathatomically:YES];
NSLog(@"%ld",(long)indexPath.row);
//移除圖片的下載緩存
[self.operationremoveObjectForKey:app.icon];
}];
//添加操作到操作緩存中
[self.operationsetObject:downloadforKey:app.icon];
//添加操作到隊列
[self.queueaddOperation:download];
}
}
}
//打印沙盒路徑
//NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES));
returncell;
}
//UI很不流暢-》開子線程下載
//圖片重復下載-》緩存
//內存緩存-》磁盤緩存
//圖片不會刷新:手動刷新,因為圖片尺寸為0
//圖片數(shù)據(jù)錯亂
//圖片重復下載:當圖片還為完全下載之前记劝,又要重新展示圖片
//documents:手機連上itunes會備份变姨,不允許把緩存數(shù)據(jù)放到這個路徑。
//library:緩存路徑:保存緩存數(shù)據(jù)偏好設置:保存一些賬號信息
//tmp:臨時路徑厌丑,隨時會被刪除
@end
//如果出現(xiàn)內存警告
- (void)didReceiveMemoryWarning{
//不會影響圖片顯示
[self.imageDicremoveAllObjects];
//取消隊列中所有操作
[self.queuecancelAllOperations];
}