OC 线程操作3 - NSOperation 实现线程间通信
#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 实现线程间通信的更多相关文章
- OC 线程操作3 - NSOperation
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- PYTHON线程知识再研习G--线程间通信Event
很多时候,线程之间会有互相通信的需要.常见的情形是次要线程为主要线程执行特定的任务,在执行过程中需要不断报告执行的进度情况.前面的条件变量同步已经涉及到了线程间的通信(threading.Condit ...
- Winform跨线程操作界面的策略
BeginInvoke(new ThreadStart(() => toolStripButton1.Text = "aaa")); 1.非跨线程操作和部分跨线程get不会引 ...
- 8)Linux程序设计入门--线程操作
)Linux程序设计入门--线程操作 前言:Linux下线程的创建 介绍在Linux下线程的创建和基本的使用. Linux下的线程是一个非常复杂的问题,由 于我对线程的学习不时很好,我在这里只是简单的 ...
- iOS开发NSOperation 三:操作依赖和监听以及线程间通信
一:操作依赖和监听 #import "ViewController.h" @interface ViewController () @end @implementation Vie ...
- 线程间通信的三种方式(NSThread,GCD,NSOperation)
一.NSThread线程间通信 #import "ViewController.h" @interface ViewController ()<UIScrollViewDel ...
- OC 线程操作2 - NSThread
方法1 :直接创建 alloc init - (void)createNSThread111{ /* 参数1: (nonnull id) 目标对象 self 参数2:(nonnull SEL) ...
- OC 线程操作 - GCD队列组
1.队列组两种使用方法2.队列组等待 wait /** 新方法 队列组一般用在在异步操作,在主线程写队列组毫无任何作用 */ - (void)GCD_Group_new_group___notify{ ...
- OC 线程操作 - GCD快速迭代
- (void)forDemo{ //全都是在主线程操作的 ; i<; i++) { NSLog(@"--%@", [NSThread currentThread]); } ...
随机推荐
- 自己写的jQuery颜色插件
界面效果: 插件js代码: ;(function ($) { //122种颜色 var aColors = [ "ff0000", "ffff00", &quo ...
- Ubuntu-14.04.1 desktop安装时遇到的小问题
su root认证失败:sudo passwd root,然后设置新密码. 重装linux导致g++显示已安装,但无法使用:将"系统设置"/"软件源"中所有更新 ...
- spark1.0属性配置以及spark-submit简单使用
在spark1.0中属性支持三种配置方式: 1.代码 在代码中构造SparkConf时指定master.appname或者key-value等 val conf = new SparkConf(); ...
- 安装HBase(0.9)数据库
基本知识: 1.hbase是一种基于列存储的数据库,也就是说它的一列的数据是存储在一个文件里面的,而传统的数据库存储都是一个文件存储多个行,这些行有不同的列,这些列的数据类型 不同. 2.基于HDFS ...
- webpack(4)--module
Module module的配置如何处理模块. 配置Loader rules 配置模块的读取和解析规则, 通常用来配置loader, 其类型是一个数组, 数组里每一项都描述了如何去处理部分文件. 配置 ...
- jieba库及wordcloud库的使用
知识内容: 1.jieba库的使用 2.wordcloud库的使用 参考资料: https://github.com/fxsjy/jieba https://blog.csdn.net/fontthr ...
- THINKPHP3.2.3增加阿里云短信接口思路整理
https://help.aliyun.com/document_detail/55359.html?spm=5176.product44282.4.7.O4lc1n 阿里云短信服务地址,感冒的下载看 ...
- springboot sybase 数据库
依赖:(驱动) <!-- https://mvnrepository.com/artifact/net.sourceforge.jtds/jtds --> <dependency&g ...
- mysql的时区错误的解决办法
十二月 02, 2018 9:16:19 下午 com.mchange.v2.resourcepool.BasicResourcePool 警告: Having failed to acquire a ...
- VBA 编写类
一.初识类 现在,请打开你的VBE,主菜单-插入-类模块. 插入了一个类模块,也就建立了一个类.类模块的名字就是类的名字.你现在看到的,她的名字叫“类1”,这是VBA按她姐妹排行给她取的的,是的,VB ...