iOS多圖下載案例(三)

利用多線程進(jìn)行優(yōu)化

#import"ViewController.h"

#import"App.h"

@interfaceViewController()

@property(nonatomic,strong)NSArray*apps;

//內(nèi)存緩存

@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];

//設(shè)置最大并發(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;

//下載圖片

//先查看圖片在內(nèi)存緩存中是否存在,如果存在,直接拿來(lái)用,但程序重新啟動(dòng)時(shí),還要重新下載

//如果有磁盤緩存把磁盤緩存放到內(nèi)存緩存,否則直接下載

//1.沒有下載過(guò)

//2.下載過(guò)被銷毀了

UIImage*imaged = [self.imageDicobjectForKey:app.icon];

if(imaged) {

cell.imageView.image=imaged;

NSLog(@"%ld---內(nèi)存緩存",(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);

//把磁盤緩存放到內(nèi)存緩存

[self.imageDicsetObject:imageforKey:app.icon];

}else{

//檢查該操作是否在緩存中,如果是就什么也不做

NSBlockOperation*download = [self.operationobjectForKey:app.icon];

if(download) {

}else{

//先清空image,放個(gè)占位的圖片

cell.imageView.image=[UIImageimageNamed:@"Snip20170530_1"];

download = [NSBlockOperationblockOperationWithBlock:^{

NSURL*url = [NSURLURLWithString:app.icon];

NSData*imageData = [NSDatadataWithContentsOfURL:url];

UIImage*image = [UIImageimageWithData:imageData];

//容錯(cuò)處理,防止url不正確,或者網(wǎng)絡(luò)問(wèn)題

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;

}];

//把圖片保存在內(nèi)存緩存里

[self.imageDicsetObject:imageforKey:app.icon];

[imageDatawriteToFile:fullpathatomically:YES];

NSLog(@"%ld",(long)indexPath.row);

//移除圖片的下載緩存

[self.operationremoveObjectForKey:app.icon];

}];

//添加操作到操作緩存中

[self.operationsetObject:downloadforKey:app.icon];

//添加操作到隊(duì)列

[self.queueaddOperation:download];

}

}

}

//打印沙盒路徑

//NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES));

returncell;

}

//UI很不流暢-》開子線程下載

//圖片重復(fù)下載-》緩存

//內(nèi)存緩存-》磁盤緩存

//圖片不會(huì)刷新:手動(dòng)刷新,因?yàn)閳D片尺寸為0

//圖片數(shù)據(jù)錯(cuò)亂

//圖片重復(fù)下載:當(dāng)圖片還為完全下載之前,又要重新展示圖片

//documents:手機(jī)連上itunes會(huì)備份,不允許把緩存數(shù)據(jù)放到這個(gè)路徑。

//library:緩存路徑:保存緩存數(shù)據(jù)偏好設(shè)置:保存一些賬號(hào)信息

//tmp:臨時(shí)路徑,隨時(shí)會(huì)被刪除

@end

//如果出現(xiàn)內(nèi)存警告

- (void)didReceiveMemoryWarning{

//不會(huì)影響圖片顯示

[self.imageDicremoveAllObjects];

//取消隊(duì)列中所有操作

[self.queuecancelAllOperations];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容