iOS学习笔记-121.多线程20——NSOperation图片下载合成示例

  • 多线程20NSOperation图片下载合成示例
    • 一图示
    • 二分析
    • 三代码

多线程20——NSOperation图片下载合成示例

一、图示

iOS学习笔记-121.多线程20——NSOperation图片下载合成示例_第1张图片


二、分析

和本例相似的案例


iOS学习笔记-104.多线程03——线程间通信图片下载与时间计算

iOS学习笔记-107.多线程06——CGD线程间通信下载图片

iOS学习笔记-111.多线程10——CGD队列组图片下载合成示例


特别是 iOS学习笔记-111.多线程10——CGD队列组图片下载合成示例,和本例的效果是一样,只是我们使用的实现方式不同而已。


我们现在要干的事情是:下载两张图片,下载好了以后合成这两张图片。

事情的操作有先后顺序。

分析过程:

  1. 我们开启两个子线程去下载两张图片(可以放到不同的操作中)

  2. 下载完成后合并图片(可以添加依赖)

  3. 合成完成以后,主线程更新UI(提交任务到主队列中)


三、代码

//
//  QWMViewController5.m
//  03_UIview85多线程_NSOperation
//
//  Created by 杞文明 on 17/9/4.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "QWMViewController5.h"

@interface QWMViewController5 ()
@property(nonatomic,strong)UIImage *image1;
@property(nonatomic,strong)UIImage *image2;
@property(nonatomic,strong)UIImageView *imageView1;
@property(nonatomic,strong)UIImageView *imageView2;
@property(nonatomic,strong)UIImageView *imageView3;
@end

@implementation QWMViewController5

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createImageView];
}

/*创建ImageView*/
-(void)createImageView{
    //imageView1
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 150, 150)];
    [self.view addSubview:imageView];
//    imageView.backgroundColor = [UIColor redColor];
    self.imageView1 = imageView;

    //imageView2
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(200, 20, 150, 150)];
    [self.view addSubview:imageView];
//    imageView.backgroundColor = [UIColor redColor];
    self.imageView2 = imageView;

    //imageView3
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 250, 200, 400)];
    [self.view addSubview:imageView];
//    imageView.backgroundColor = [UIColor redColor];
    self.imageView3 = imageView;
}

/*根据地址获取到图片*/
-(UIImage*)downloadImage:(NSString*)path{
    NSURL * url = [NSURL URLWithString:path];
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    return [UIImage imageWithData:imageData];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self comBie];
}

-(void)comBie{
    //创建队列
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];

    //1.下载第一个图片
    NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
        NSString *path = @"http://cdn.duitang.com/uploads/item/201502/09/20150209175629_iraer.jpeg";
        _image1 = [self downloadImage:path];
    }];
    //2.下载第二个图片
    NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
        NSString *path = @"http://v1.qzone.cc/avatar/201507/30/15/22/55b9d0d1709da731.png!200x200.jpg";
        _image2 = [self downloadImage:path];
    }];
    //3.合并
    NSBlockOperation *combie = [NSBlockOperation blockOperationWithBlock:^{
        //1.开启图片上下文
        UIGraphicsBeginImageContext(CGSizeMake(200, 400));

        //2.画图
        [self.image1 drawInRect:CGRectMake(0, 0, 200, 200)];
        [self.image2 drawInRect:CGRectMake(0, 200, 200, 200)];
        //加水印
        UIImage *image = [UIImage imageNamed:@"qiwenminglog"];
        [image drawInRect:CGRectMake(0, 0, 200, 40)];
        [image drawInRect:CGRectMake(0, 160, 400, 80)];
        [image drawInRect:CGRectMake(0, 360, 200, 40)];

        //3.根据上下文得到图片
         image = UIGraphicsGetImageFromCurrentImageContext();

        //4.关闭上下文
        UIGraphicsEndImageContext();

        //5.更新UI 主线程更新UI
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.imageView1.image = self.image1;
            self.imageView2.image = self.image2;
            self.imageView3.image = image;
        }];
    }];

    //添加依赖
    [combie addDependency:op1];
    [combie addDependency:op2];

    //添加到队列宗
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:combie];
}
@end

你可能感兴趣的:(iOS学习-iOS)