Objective-c 多线程操作 自定义NSOperation 模拟下载

写在前面
弄了下个人站...防止内容再次被锁定...所有东西都在这里面
welcome~
个人博客

写在前面

使用多线程下载图片,使用内存缓存和磁盘缓存。
这里只为理解NSOperation及其派生类
真要应用到APP中 请下载成熟的第三方库

效果

效果

下载多张图片时可控制线程并发数

分析

  • 自定义NSOperation 执行下载操作
  • 封装一个队列操作,创建N个线程,队列控制并发
  • 通过线程tag查找对应图片
  • 通过队列tag查找对应队列
  • 缓存设置 内存缓存和磁盘缓存
  • 下载完成通过delegate或block进行回调通知

代码

  • 通用内容 线程完成时的回调 、加载图片时的回调
#ifndef Uinty_h
#define Uinty_h
#import <UIKit/UIKit.h>

typedef void(^DownloadImageDataBlock)(NSData *data,int tag);
typedef void(^DownloadImageBlock)(UIImage *image,int tag,int queueTag);

static int const  kImageViewTag = 1990;
//线程操作协议
@protocol DownloadOperationDelegate <NSObject>
//线程下载数据完成
- (void)downloadOperationWithData:(NSData*)data withTag:(int)tag;

@end

//下载操作协议
@protocol DownloadImageDelegate <NSObject>
//图片回调
- (void)downloadImageFinishedWith:(UIImage*)image andTag:(int)tag withQueueTag:(int)queueTag;
@end
#endif /* Uinty_h */
  • 自定义NSOperation 下载数据
#import <Foundation/Foundation.h>
#import "Uinty.h"
@interface DownloadOperation : NSOperation

//block
@property (nonatomic,copy)DownloadImageDataBlock imageDataBlock;
//标识
@property (nonatomic,assign)int tag;

//代理
@property (nonatomic,strong)id<DownloadOperationDelegate> delegate;

//初始化
- (instancetype)initWithUrlStr:(NSString*)urlStr;

+ (instancetype)downloadOperationWithUrlStr:(NSString*)urlStr;
@end


#import "DownloadOperation.h"
@interface DownloadOperation()
@property(nonatomic,copy)NSString *urlStr;

@end
@implementation DownloadOperation

- (instancetype)initWithUrlStr:(NSString*)urlStr {
    self = [super init];
    if (self) {
        self.urlStr = urlStr;
    }
    return self;
}

+ (instancetype)downloadOperationWithUrlStr:(NSString*)urlStr {
    return [[DownloadOperation alloc] initWithUrlStr:urlStr];
}

-(void)main {

    NSURL *url = [NSURL URLWithString:self.urlStr];
    NSData *data = [NSData dataWithContentsOfURL:url];
    //模拟耗时
    sleep(1);
    //返回主线程
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        //block通知
        if (self.imageDataBlock) {
            self.imageDataBlock(data,self.tag);
        }
        //代理通知
        if ([self.delegate respondsToSelector:@selector(downloadOperationWithData:withTag:)]) {
            [self.delegate downloadOperationWithData:data withTag:self.tag];
        }
    }];
    }
@end
  • 加载图片:缓存加载和线程队列加载
#import <Foundation/Foundation.h>
#import "DownloadOperation.h"
@interface DownloadImage : NSObject
//加载完成
@property (nonatomic,copy)DownloadImageBlock downloadFinishedBlock;
//加载单张时使用
@property (nonatomic,copy)NSString *urlStr;
//加载多张时使用
@property (nonatomic,strong)NSArray *urlArray;
//队列线程最大并发数
@property (nonatomic,assign)int  maxOperationCount;
//代理
@property (nonatomic,strong)id<DownloadImageDelegate> delegate;
//标识
@property (nonatomic,assign)int tag;
//磁盘缓存 内存缓存 单位:M
@property (nonatomic,assign)NSUInteger diskCapacity;
@property (nonatomic,assign)NSUInteger MemoryCapacity;

//初始化传url数组
- (instancetype)initWithUrlStrArray:(NSArray<NSString*>*)urlArray withStartTag:(int)startTag ;
//初始化单url
- (instancetype)initWithUrlStr:(NSString*)urlStr ;
//类工厂
+ (instancetype)downloadImageWithUrlStrArray:(NSArray<NSString*>*)urlArray withStartTag:(int)startTag ;
+ (instancetype)downloadImageWithUrlStr:(NSString*)urlStr ;

//开始下载
- (void)starDownloadImage;
@end
//
//  DownloadImage.m
//  DownloadImageDemo
//
//  Created by gongwenkai on 2017/1/3.
//  Copyright © 2017年 gongwenkai. All rights reserved.
//

#import "DownloadImage.h"
#import "DownloadOperation.h"


@interface DownloadImage()
@property (nonatomic,strong)NSOperationQueue *queue;
@property (nonatomic,strong)NSCache *imageCache;
@property (nonatomic,copy)NSString *cachePath;
@property (nonatomic,assign)int imageStartTag;

@end

@implementation DownloadImage

#pragma mark - 懒加载
- (NSString *)cachePath {
    if (!_cachePath) {
        _cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    }
    return _cachePath;
}

- (NSCache *)imageCache {
    if (!_imageCache) {
        _imageCache = [[NSCache alloc] init];
        _imageCache.countLimit = 100;
        
    }
    return _imageCache;
}

- (NSOperationQueue *)queue {
    if (!_queue) {
        _queue = [[NSOperationQueue alloc] init];
    }
    return _queue;
}

- (NSUInteger)MemoryCapacity {
    if (!_MemoryCapacity) {
        _MemoryCapacity = 1;
    }
    return _MemoryCapacity;
}

- (NSUInteger)diskCapacity {
    if (!_diskCapacity) {
        _diskCapacity = 10;
    }
    return _diskCapacity;
}

- (int)maxOperationCount {
    if (!_maxOperationCount) {
        _maxOperationCount = 2;
    }
    return _maxOperationCount;
}

#pragma mark - 初始化
+ (instancetype)downloadImageWithUrlStrArray:(NSArray<NSString*>*)urlArray withStartTag:(int)startTag {
    return [[DownloadImage alloc] initWithUrlStrArray:urlArray withStartTag:startTag];
}

+ (instancetype)downloadImageWithUrlStr:(NSString*)urlStr {
    return [[DownloadImage alloc] initWithUrlStr:urlStr];
}

- (instancetype)initWithUrlStrArray:(NSArray<NSString*>*)urlArray withStartTag:(int)startTag{
    self = [super init];
    if (self) {
        self.urlArray = urlArray;
        self.imageStartTag = startTag;
    }
    return self;
}

- (instancetype)initWithUrlStr:(NSString*)urlStr {
    self = [super init];
    if (self) {
        self.urlStr = urlStr;
    }
    return self;
}

//初始化 set方法 单个url装进数组
- (void)setUrlStr:(NSString *)urlStr {
    _urlStr = urlStr;
    _urlArray = [NSArray arrayWithObject:urlStr];
}


#pragma mark - 加载图片
- (void)starDownloadImage {
    
    //设置内存缓存和磁盘缓存大小
    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:self.MemoryCapacity * 1024 * 1024 diskCapacity:self.diskCapacity * 1024 * 1024 diskPath:nil];
    [NSURLCache setSharedURLCache:URLCache];

    //加载数据
    for (int i = 0; i < self.urlArray.count; i++) {
        int imageTag = self.imageStartTag + i;
        NSString *urlStr = self.urlArray[i];
        //从内存缓存中读取图片
        UIImage *memoryImage = [self.imageCache objectForKey:urlStr];
        if (memoryImage) {
            //block回调结果
            if (self.downloadFinishedBlock) {
                self.downloadFinishedBlock(memoryImage,imageTag,self.tag);
            }
            //代理回调结果
            if ([self.delegate respondsToSelector:@selector(downloadImageFinishedWith:andTag:withQueueTag:)]) {
                [self.delegate downloadImageFinishedWith:memoryImage andTag:imageTag withQueueTag:self.tag];
            }
            continue;
        }
        //从磁盘缓存中读取图片
        NSString *imagePath=[urlStr lastPathComponent];
        NSString *imageCachePath = [self.cachePath stringByAppendingPathComponent:imagePath];
        NSData *data = [NSData dataWithContentsOfFile:imageCachePath];
        if (data) {
            UIImage *diskImage = [UIImage imageWithData:data];
            //block回调结果
            if (self.downloadFinishedBlock) {
                self.downloadFinishedBlock(diskImage,imageTag,self.tag);
            }
            //代理回调结果
            if ([self.delegate respondsToSelector:@selector(downloadImageFinishedWith:andTag:withQueueTag:)]) {
                [self.delegate downloadImageFinishedWith:diskImage andTag:imageTag withQueueTag:self.tag];
            }
            continue;
        }
        
        
        //创建线程加载图片
        self.queue.maxConcurrentOperationCount = self.maxOperationCount;
        DownloadOperation *op = [DownloadOperation downloadOperationWithUrlStr:urlStr];
        
        op.tag = i + kImageViewTag;
        if (self.urlArray.count < self.maxOperationCount) {
            [op start];
        }else {
            [self.queue addOperation:op];
        }
    
        //线程回调结果
        op.imageDataBlock = ^(NSData *data,int tag){
            UIImage *image = [UIImage imageWithData:data];
            
            //写入内存缓存
            [self.imageCache setObject:image forKey:urlStr];
            //写入磁盘缓存
            [data writeToFile:imageCachePath atomically:YES];
            
            //block回调
            if (self.downloadFinishedBlock) {
                NSLog(@"tage ======%d",tag);
                self.downloadFinishedBlock(image,tag,self.tag);
            }
            //代理回调
            if ([self.delegate respondsToSelector:@selector(downloadImageFinishedWith:andTag:withQueueTag:)]) {
                [self.delegate downloadImageFinishedWith:image andTag:tag withQueueTag:self.tag];
            }
            
        };

    }
    
}

@end

  • 外部调用及测试
- (void)viewDidLoad {
    [super viewDidLoad];

    for (int i = 0; i < 6; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, i*105, 100, 100)];
        imgView.tag = i + kImageViewTag;
        imgView.backgroundColor = [UIColor redColor];
        [self.view addSubview:imgView];
    }
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 100, 100, 100)];
    imgView.backgroundColor = [UIColor redColor];
    imgView.tag = 1234;
    [self.view addSubview:imgView];
    NSLog(@"%@",NSHomeDirectory());
}

- (IBAction)clickLoadImages:(id)sender {
    //下载多张图片
    DownloadImage *download = [DownloadImage downloadImageWithUrlStrArray:@[@"http://img.hb.aicdn.com/6e004cb3c5f58f016a57b90f8bbb93d7075453f2efd0-anTckt_fw658",@"http://img.hb.aicdn.com/fb18b522caf2821adb7af96f8656787f8d9bdad31bdec-6f6h4X_fw658",@"http://img.hb.aicdn.com/22e4dfd8d135de7ae0d451e351c00bddf732919920840-BMRw4Z_fw658",@"http://img.hb.aicdn.com/536c96af48b38faca5bcad20ba0ea6aba8929b711e5b4-lvdBlI_fw658",@"http://img.hb.aicdn.com/055e5458bd340a52ca0067f5d7c22b6c3b18d119292ae-QLEsYp_fw658",@"http://img.hb.aicdn.com/b50481ab8a2b4e3587068df0552ebad08409f0b3ca23-8gBQ9x_fw658"] withStartTag:kImageViewTag];
    //设置并发数
    download.maxOperationCount = 3;
    download.tag = 0;
    download.delegate = self;
    /*   block 回调 结果
     download.downloadFinishedBlock = ^(UIImage *image,int tag,int queueTag) {
     UIImageView *img = [self.view viewWithTag:tag];
     img.image = image;
     NSLog(@"jicia====%d",tag);
     };
     */
    //开始下载
    [download starDownloadImage];
}

- (IBAction)clickLoadSingalImage:(id)sender {
    //单张下载
    DownloadImage *down = [DownloadImage downloadImageWithUrlStr:@"http://img.hb.aicdn.com/b50481ab8a2b4e3587068df0552ebad08409f0b3ca23-8gBQ9x_fw658"];
    down.tag = 1;
    down.delegate = self;
    
    /*   block 回调 结果
     down.downloadFinishedBlock = ^(UIImage *image,int tag,int queueTag) {
     UIImageView *imgV = [self.view viewWithTag:1234];
     imgV.image = image;
     };
     */
    [down starDownloadImage];
    
}
- (IBAction)cleanAll:(id)sender {
    
    for (int i = 0; i < 6; i++) {
        UIImageView *imgView = [self.view viewWithTag:i + kImageViewTag];
        imgView.image = nil;
    }
    UIImageView *imgView = [self.view viewWithTag:1234];
    imgView.image = nil;
    
}

//通过代理回调操作
- (void)downloadImageFinishedWith:(UIImage*)image andTag:(int)tag withQueueTag:(int)queueTag{
    if (queueTag == 0) {
        UIImageView *img = [self.view viewWithTag:tag];
        img.image = image;
    } else if (queueTag == 1) {
        UIImageView *imgV = [self.view viewWithTag:1234];
        imgV.image = image;
    }

}

DEMO地址

https://github.com/gongxiaokai/DownloadImageDemo

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

推荐阅读更多精彩内容