iOS多图下载案例(二)

利用缓存优化后

//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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 利用多线程进行优化 #import"ViewController.h"#import"App.h"@interfa...
    BEYOND黄阅读 2,695评论 0 5
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,382评论 30 472
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,963评论 3 38
  • 离群索居的日子又在眼前, 之前那飘忽的一段只能算一转眼。 脑海中存有多少个相似的画面, 却始终没能突破那些局限。 ...
    艺林漫笔阅读 1,261评论 0 0
  • 和黏糕先生认识八年,走过热恋,平静,争吵,到现在开始弄浆涮洗开始满是生活味道的小日子。 上大学时,我特别爱和舍友分...
    双丝芊芊节阅读 3,301评论 0 3