利用缓存优化后
//UI很不流畅-》开子线程下载
//图片重复下载-》缓存
//内存缓存-》磁盘缓存
//documents:手机连上itunes会备份,不允许把缓存数据放到这个路径。
//library:缓存路径:保存缓存数据 偏好设置:保存一些账号信息
//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;
//内存缓存
@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;
//下载图片
//先查看图片在内存缓存中是否存在,如果存在,直接拿来用,但程序重新启动时,还要重新下载
//如果有磁盘缓存把磁盘缓存放到内存缓存,否则直接下载
//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{
NSURL*url = [NSURLURLWithString:app.icon];
NSData*imageData = [NSDatadataWithContentsOfURL:url];
UIImage*image = [UIImageimageWithData:imageData];
cell.imageView.image=image;
//把图片保存在内存缓存里
[self.imageDicsetObject:imageforKey:app.icon];
[imageDatawriteToFile:fullpathatomically:YES];
NSLog(@"%ld",(long)indexPath.row);
}
}
//打印沙盒路径
//NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES));
returncell;
}
//UI很不流畅-》开子线程下载
//图片重复下载-》缓存
//内存缓存-》磁盘缓存
//documents:手机连上itunes会备份,不允许把缓存数据放到这个路径。
//library:缓存路径:保存缓存数据偏好设置:保存一些账号信息
//tmp:临时路径,随时会被删除
@end
网友评论