利用緩存優(yōu)化后
//UI很不流暢-》開子線程下載
//圖片重復(fù)下載-》緩存
//內(nèi)存緩存-》磁盤緩存
//documents:手機(jī)連上itunes會(huì)備份瞪慧,不允許把緩存數(shù)據(jù)放到這個(gè)路徑潦蝇。
//library:緩存路徑:保存緩存數(shù)據(jù) ? 偏好設(shè)置:保存一些賬號(hào)信息
//tmp:臨時(shí)路徑痢缎,隨時(shí)會(huì)被刪除
//
//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)存緩存中是否存在,如果存在梁只,直接拿來用,但程序重新啟動(dòng)時(shí),還要重新下載
//如果有磁盤緩存把磁盤緩存放到內(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會(huì)備份,不允許把緩存數(shù)據(jù)放到這個(gè)路徑旭斥。
//library:緩存路徑:保存緩存數(shù)據(jù)偏好設(shè)置:保存一些賬號(hào)信息
//tmp:臨時(shí)路徑容达,隨時(shí)會(huì)被刪除
@end