#import "ViewController.h"

 @interface ViewController ()

 /**
图片
*/
@property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// [self downloadImage];
[self downloadImagAndCompoundIma];
} /**
下载图片 并且合成
  */
-(void)downloadImagAndCompoundIma{ /*
获取图片属性1:搞两个属性 然后self.img1, self.img2
获取图片属性2: 使用__blcok修饰,
*/
__block UIImage *ima1 = [[UIImage alloc] init];
__block UIImage *ima2 = [[UIImage alloc] init]; //1.创建 非主队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; //2.创建任务 : 下载图片1
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{ NSString *urlStr = [NSString stringWithFormat:@"http://pic.90sjimg.com/design/00/07/85/23/58ef8faf71ffe.png"];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
ima1 = [UIImage imageWithData:data];
}]; //3.创建任务 : 下载图片2
NSBlockOperation * op2 = [NSBlockOperation blockOperationWithBlock:^{
// 下载图片
NSString *urlStr = [NSString stringWithFormat:@"http://pic28.nipic.com/20130330/9607253_143631959000_2.png"];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
ima2 = [UIImage imageWithData:data];
}]; //4.合成图片
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
//1。开启图形上下文 并且设置上下文 宽高
UIGraphicsBeginImageContext(CGSizeMake(, )); //2.图片画图
[ima1 drawInRect:CGRectMake(, , , )];
ima1 = nil; [ima2 drawInRect:CGRectMake(, , , )];
ima2 = nil; //3.根据图形上下文去图片
UIImage *ima = UIGraphicsGetImageFromCurrentImageContext(); //4.关闭上下文
UIGraphicsEndImageContext();
//3.回到主线程刷新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = ima;
}];
}]; //5.添加依赖,因为全都是异步执行 谁先谁后不可控
[op3 addDependency:op1];
[op3 addDependency:op2]; //3.添加任务
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
} /**
下载图片
  */
-(void)downloadImage{ //1.创建 非主队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; //2.创建任务
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{ //2.1 下载图片
NSString *urlStr = [NSString stringWithFormat:@"http://pic.90sjimg.com/design/00/07/85/23/58ef8faf71ffe.png"];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *ima = [UIImage imageWithData:data]; //3.回到主线程刷新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = ima;
}]; }]; //3.添加任务
[queue addOperation:op1];
}
@end

OC 线程操作3 - NSOperation 实现线程间通信的更多相关文章

  1. OC 线程操作3 - NSOperation

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  2. PYTHON线程知识再研习G--线程间通信Event

    很多时候,线程之间会有互相通信的需要.常见的情形是次要线程为主要线程执行特定的任务,在执行过程中需要不断报告执行的进度情况.前面的条件变量同步已经涉及到了线程间的通信(threading.Condit ...

  3. Winform跨线程操作界面的策略

    BeginInvoke(new ThreadStart(() => toolStripButton1.Text = "aaa")); 1.非跨线程操作和部分跨线程get不会引 ...

  4. 8)Linux程序设计入门--线程操作

    )Linux程序设计入门--线程操作 前言:Linux下线程的创建 介绍在Linux下线程的创建和基本的使用. Linux下的线程是一个非常复杂的问题,由 于我对线程的学习不时很好,我在这里只是简单的 ...

  5. iOS开发NSOperation 三:操作依赖和监听以及线程间通信

    一:操作依赖和监听 #import "ViewController.h" @interface ViewController () @end @implementation Vie ...

  6. 线程间通信的三种方式(NSThread,GCD,NSOperation)

    一.NSThread线程间通信 #import "ViewController.h" @interface ViewController ()<UIScrollViewDel ...

  7. OC 线程操作2 - NSThread

        方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...

  8. OC 线程操作 - GCD队列组

    1.队列组两种使用方法2.队列组等待 wait /** 新方法 队列组一般用在在异步操作,在主线程写队列组毫无任何作用 */ - (void)GCD_Group_new_group___notify{ ...

  9. OC 线程操作 - GCD快速迭代

    - (void)forDemo{ //全都是在主线程操作的 ; i<; i++) { NSLog(@"--%@", [NSThread currentThread]); } ...

随机推荐

  1. ni_set()函数的使用 以及 post_max_size,upload_max_filesize的修改方法

    Apache服务器处理: ini_set('display_errors', 'Off');ini_set('memory_limit', -1); //-1 / 10240Mini_set(&quo ...

  2. Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本)

    Hive环境的安装部署(完美安装)(集群内或集群外都适用)(含卸载自带mysql安装指定版本) Hive 安装依赖 Hadoop 的集群,它是运行在 Hadoop 的基础上. 所以在安装 Hive 之 ...

  3. .CBB 文件 如何打开

    CCB Visual Basic动态按钮配置文件 用SQL server,导入时选择 access,打开后即可.

  4. webpack(5)--Resolve

    Resolve webpack在启动后会从配置的入口模块触发找出所有依赖的模块,Resolve配置webpack如何寻找模块对应的文件.webpack内置JavaScript模块化语法解析功能,默认会 ...

  5. 经典算法 Manacher算法详解

    内容: 1.原始问题   =>O(N^2) 2.Manacher算法   =>O(N) 1.原始问题 Manacher算法是由题目“求字符串中长回文子串的长度”而来.比如 abcdcb 的 ...

  6. 一,Android Studio笔记

    转自:https://developer.android.com/studio/intro/index.html 一.界面 Android Studio 主窗口由图 3 标注的几个逻辑区域组成. 工具 ...

  7. linux 下创建文本的方法

    1.   打开终端,输入  vi  1.txt 输入:wq 文本创建成功 2.  打开终端,输入  vim  1.txt 输入:wq 文本创建成功 3. 打开终端 , 输入 touch  1.txt ...

  8. WebService客户端调用的几种方式

    1.用组件HTTPRIO,支持VCL WIN32和FIRE MONKEY跨平台Android手机 HTTPRIO1.URL := 'http://127.0.0.1:8080/soap/IsoapTe ...

  9. CategoryPanelGroup动态生成节点

    afw TCategoryPanel *cp; ; i < TreeView1->Items->Count; i++) { ) { cp = new TCategoryPanel(C ...

  10. CentOS Tomcat启动 Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

    链接:http://blog.csdn.net/shangdiyisi/article/details/9477521 [bravoinfo@bravoinfo-hk-01 apache-tomcat ...