您的位置:首页 > 理论基础 > 计算机网络

iOS如何获取网络图片(三)有沙盒的

2016-03-17 16:50 513 查看

沙盒

沙盒简介

默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件

Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录,如果保存了下载的数据,程序提交会被直接被拒绝

Library:存储程序的默认设置或其它状态信息;

Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

Library/Preferences:偏好设置文件

tmp:提供一个即时创建临时文件的地方,在iPhone在重启时,会丢弃所有的tmp文件。

获取沙盒目录

//在模拟器上,沙盒目录是变化的,所以每次都要打印

// NSString * path = NSHomeDirectory();

NSArray * array = @[@1,@2,@3,@4,@5];

//获取沙盒目录的方法

NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

//追加文件名

path = [path stringByAppendingPathComponent:@"data.plist"];

[array writeToFile:path atomically:YES];

NSLog(@"%@",path);

系统偏好设置代码

//偏好设置,用户信息,是否推送,是否支持3G.
NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
//存储偏好数据
//    [ud setObject:@"11" forKey:@"age"];
//
//    [ud setInteger:20 forKey:@"weight"];
//
//    //    setObject + synchronize
//    //马上存入本地
//    [ud synchronize];

//从本地获取
//    NSLog(@"%@", [ud objectForKey:@"username"]);

[ud removeObjectForKey:@"age"];

有沙盒的网络请求

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

SXTShopCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];

SXTShop * shop = self.dataList[indexPath.row];

cell.shop = shop;

//为了避免重复加载的问题,创建了downloadImage,downloadImage属于数据源,当tableview滚动的时候就可以给cell的数据赋值

//从图片缓冲池里找到对应的图片
if ([self.imageCache objectForKey:shop.shop_image]) {

//如果下载过,直接从内存中获取图片
//        cell.iconView.image = shop.downloadImage;
cell.iconView.image = self.imageCache[shop.shop_image];

} else {

//从本地获取缓存图片
UIImage * image = [UIImage imageWithContentsOfFile:[self cacheWithPathUrl:shop.shop_image]];

//如果本地存在
if (image) {

//将本地图片放入内存中
[self.imageCache setObject:image forKey:shop.shop_image];

cell.iconView.image = image;

} else {

//设置默认图片
cell.iconView.image = [UIImage imageNamed:@"defaultImage"];

[self downloadImage:indexPath];
}
}

return cell;
}

- (void)downloadImage:(NSIndexPath *)indexPath {

SXTShop * shop = self.dataList[indexPath.row];

if ([self.operationDict objectForKey:shop.shop_image]) {

NSLog(@"已经请求过了,请等待下载");

} else {

//如果未下载过,开启异步线程
NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock:^{

//模拟网络延时
[NSThread sleepForTimeInterval:1];

//通过url获取网络数据
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",baseUrl,shop.shop_image]]];

//将数据装换为图片
UIImage * image = [UIImage imageWithData:data];

//如果有图片
if (image) {

//通知model,将图片赋值给downloadImage,以便下次从内存获取
//                    shop.downloadImage = image;

//将图片作为value,将url作为key
[self.imageCache setObject:image forKey:shop.shop_image];

//将网络获取的图片存入沙盒
[data writeToFile:[self cacheWithPathUrl:shop.shop_image] atomically:YES];

//获取主队列,更新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{

//刷新第indexPath单元的表格
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

}];
}
}];

//将请求加入操作缓冲池中
[self.operationDict setObject:op forKey:shop.shop_image];

//将请求加入全局队列
[self.queue addOperation:op];
}
}

- (NSString *)cacheWithPathUrl:(NSString *)netUrl {

//获取沙盒路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

//获取url的最后一项路径和path拼接
path = [path stringByAppendingPathComponent:[netUrl lastPathComponent]];

return path;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.tableView];
}

//当内存发生警报时,调用
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

[self.imageCache removeAllObjects];
[self.operationDict removeAllObjects];

[self.queue cancelAllOperations];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: