该文章只列出 与 : http://www.jianshu.com/p/835c33058b11 不同之处
ZYXDownloadOperation.m
#import "ZYXDownloadOperation.h"
#import "NSString+Hash.h"
@implementation ZYXDownloadOperation
/**
* 自定义NSOperation的步骤很简单
* 重写 - (void)main 方法,在里面实现想执行的任务
*/
- (void)main{
#warning - 自己创建自动释放池(因为如果是异步执行,无法访问主线程的自动释放池)
@autoreleasepool{
// 沙盒缓存
// 获得Library/Caches文件夹
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
// 获得文件名
NSString *filename = [self.urlString md5String];
// 计算出文件的全路径
NSString *file = [cachesPath stringByAppendingPathComponent:filename];
NSLog(@"file path = %@",file);
// 加载沙盒的文件数据
NSData *data = [NSData dataWithContentsOfFile:file];
UIImage *image = nil;
if (data) { // 直接利用沙盒中图片
image = [UIImage imageWithData:data];
}
else{
NSURL *downloadUrl = [NSURL URLWithString:self.urlString];
data = [NSData dataWithContentsOfURL:downloadUrl]; // 这行会比较耗时
// url -> md5 作为文件名写入沙盒
[data writeToFile:file atomically:YES];
image = [UIImage imageWithData:data];
}
if ([self.delegate respondsToSelector:@selector(downloadOperation:didFinishDownloadWithImage:)]){
// 线程间通信,NSOperation和GCD的混合使用,子线程获取数据->主线程使用子线程获取的数据
dispatch_async(dispatch_get_main_queue(), ^{ // 回到主线程, 传递图片数据给代理对象
[self.delegate downloadOperation:self didFinishDownloadWithImage:image];
});
}
}
}
@end
ViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"app";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:ID];
}
ZYXApp *app = self.apps[indexPath.row];
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
// 显示图片
// 保证一个url对应一个ZYXDownloadOperation
// 保证一个url对应UIImage对象
UIImage *image = self.images[app.icon];
if (image) { // 缓存中有图片
cell.imageView.image = image;
}
else { // 下载图片
cell.imageView.image = [UIImage imageNamed:@"57437179_42489b0"];
ZYXDownloadOperation *operation = self.operations[app.icon];
if (operation) { // 正在下载
// ... 暂时不需要做其他事
} else { // 没有正在下载
// 创建操作
operation = [[ZYXDownloadOperation alloc] init];
operation.urlString = app.icon;
operation.delegate = self;
operation.indexPath = indexPath;
[self.queue addOperation:operation]; // 异步下载
self.operations[app.icon] = operation;
}
}
// SDWebImage : 专门用来下载图片
return cell;
}
#pragma mark - ZYXDownloadOperationDelegate
- (void)downloadOperation:(ZYXDownloadOperation *)operation didFinishDownloadWithImage:(UIImage *)image{
// 数据加载失败
if (image == nil) {
[self.operations removeObjectForKey:operation.urlString];
return;
}
// 1.移除执行完毕的操作
[self.operations removeObjectForKey:operation.urlString];
if (image) {
// 2.将图片放到缓存中(images)
self.images[operation.urlString] = image;
// 3.刷新表格
[self.tableView reloadRowsAtIndexPaths:@[operation.indexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
}
项目结构.png
MD5类似于SDWebImage下载图片的结构.png
/*
2016-08-19 19:12:27.502 04-自定义Operation[37969:703173] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/c5410ea9d3d024f25030e041650a3511
2016-08-19 19:12:27.508 04-自定义Operation[37969:703172] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/9e3f2c67bbc44015f108db002c6ab65d
2016-08-19 19:12:27.510 04-自定义Operation[37969:703171] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/725d1f6ee2a0175e01dee9cc2df79ade
2016-08-19 19:12:28.125 04-自定义Operation[37969:703173] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/12aa8f5e1dc2ee5b22c52a8e3266eef6
2016-08-19 19:12:28.126 04-自定义Operation[37969:703173] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/51f567096d6ea0abc29e0e0fba7e9d64
2016-08-19 19:12:28.129 04-自定义Operation[37969:703171] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/7f63b09190f756684920b72b6ec01b34
2016-08-19 19:12:28.131 04-自定义Operation[37969:703204] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/9d884e3fa9c548a95fd93337f3bd6584
2016-08-19 19:12:28.377 04-自定义Operation[37969:703201] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/4a2eeeb28ebef78f8b93dde3a0adcde5
2016-08-19 19:12:28.404 04-自定义Operation[37969:703204] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/93ac294b61d3f4963b9f8d97a89e33f1
2016-08-19 19:12:28.439 04-自定义Operation[37969:703172] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/dab9a4dc99e74dadfcd62b5dd4d44bb5
2016-08-19 19:12:33.964 04-自定义Operation[37969:703204] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/4a7e95bbcfd92dbc49455f6ba402e716
2016-08-19 19:12:33.964 04-自定义Operation[37969:703172] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/91824788768c2fd6394fb6736d19a549
2016-08-19 19:12:33.964 04-自定义Operation[37969:703171] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/1c0fb3c4ca72ab3d9ef2a8fab56e1c01
2016-08-19 19:12:34.268 04-自定义Operation[37969:703201] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/379bf2f694f50dff357f8b087ca1cb56
2016-08-19 19:12:34.288 04-自定义Operation[37969:703173] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/f901c63b98ba55cc07daed313ffb2c76
2016-08-19 19:12:34.520 04-自定义Operation[37969:703171] file path = /Users/admin/Library/Developer/CoreSimulator/Devices/EBFE065C-0A3A-41DA-9753-3F59FF8A154B/data/Containers/Data/Application/6F5B33B0-3774-4A95-8331-9C05CCA94DA9/Library/Caches/7c958091d16759577d953233fa7ad47e
*/