iOS多圖下載案例(二)

利用緩存優(yōu)化后

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

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

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

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

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

//tmp:臨時路徑,隨時會被刪除

//

//ViewController.m

//ManyPic

//

//Created by Apple on 2017/5/30.

//Copyright ? 2017年Apple. All rights reserved.

//

#import"ViewController.h"

#import"App.h"

@interfaceViewController()

@property(nonatomic,strong)NSArray*apps;

//內(nèi)存緩存

@property(nonatomic,strong)NSMutableDictionary*imageDic;

@end

@implementationViewController

- (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)存緩存中是否存在,如果存在,直接拿來用,但程序重新啟動時,還要重新下載

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

//1.沒有下載過

//2.下載過被銷毀了

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{

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

NSData*imageData = [NSDatadataWithContentsOfURL:url];

UIImage*image = [UIImageimageWithData:imageData];

cell.imageView.image=image;

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

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

[imageDatawriteToFile:fullpathatomically:YES];

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

}

}

//打印沙盒路徑

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

returncell;

}

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

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

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

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

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

//tmp:臨時路徑,隨時會被刪除

@end

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

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

  • 利用多線程進(jìn)行優(yōu)化 #import"ViewController.h"#import"App.h"@interfa...
    BEYOND黃閱讀 332評論 0 5
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,205評論 30 471
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,086評論 3 38
  • 離群索居的日子又在眼前, 之前那飄忽的一段只能算一轉(zhuǎn)眼。 腦海中存有多少個相似的畫面, 卻始終沒能突破那些局限。 ...
    藝林漫筆閱讀 186評論 0 0
  • 和黏糕先生認(rèn)識八年,走過熱戀,平靜,爭吵,到現(xiàn)在開始弄漿涮洗開始滿是生活味道的小日子。 上大學(xué)時,我特別愛和舍友分...
    雙絲芊芊節(jié)閱讀 480評論 0 3