//在模拟器上,沙盒目录是变化的,所以每次都要打印
// 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];
}