自定义NSOperation Delegate文件下载内存沙盒缓存 MD5

该文章只列出 与 : 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
*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,263评论 25 707
  • 作为一个前端程序猿,下面这些站会让你眼前一亮。 amazeui框架组建丰富 http://amazeui.org...
    欧巴冰冰阅读 8,802评论 18 303
  • 陈景润,印刻在一个时代的名字。褒也是他,贬也是他,傻乎乎,苦哈哈,神经兮兮,变成了数学家的形象形象代言人。七十年代...
    XinSuting阅读 338评论 0 0
  • 文/言筱妍 又是一个忙碌的周末,还没有通暖气的北方城市,冷的让人出门都有些胆怯。自从有了小宝宝,就连周末也忙于照顾...
    黎若初阅读 196评论 4 5
  • 2017.9.19 最近几天去医院照顾姥爷,日夜颠倒。昨天第二次去的时候,路已经熟悉,走着走着感觉好轻松,感谢生命...
    Ding____阅读 241评论 0 0