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]); } ...
随机推荐
- mysql存储过程的参数名不要跟字段名一样 (血淋淋的代价)
如题,将会导致的结果就是参数的值将不会是你传入的值,而是变成每条记录的那个字段的值. 这样的后果,是灰常严重的.比如执行删除操作,它能把整个表的记录全删了. 这个是我的血淋淋的代价啊. 死坑如下,勿跳 ...
- 通过Vim+少量插件配置一个高效简洁的IDE
最近本人在看<TCP/IP Illustrated Volume2:The Implementation>这本书,自然要下载4.4BSD-Lite的源代码配合书本一起研读.以前学习Vim的 ...
- 运行inetmgr提示“找不到文件”无法打开IIS管理器的解决办法
运行inetmgr提示“找不到文件”无法打开IIS管理器的解决办法 不知道什么时候开始运行inetmgr就提示找不到文件了,本以为是IIS坏了,这两天发现IIS服务还是可以运行的,只是运行inetmg ...
- 并发基础(八) java线程的中断机制
文章转载自 详细分析Java中断机制 1. 引言 当我们点击某个杀毒软件的取消按钮来停止查杀病毒时,当我们在控制台敲入quit命令以结束某个后台服务时--都需要通过一个线程去取消另一个线程正在执行的任 ...
- python学习之----BeautifulSoup示例一
BeautifulSoup 库最常用的对象恰好就是BeautifulSoup 对象. from urllib.request import urlopen from bs4 import Beauti ...
- Python 中一个逗号引发的悲剧
遇到一个 Python 字符串的坑,记录一下.看看下面这些代码 >>> a = [ ... 'foo' ... 'bar', ... 'tree' ... ] >>> ...
- sqoop2的安装配置
1.下载 wget http://mirror.bit.edu.cn/apache/sqoop/1.99.7/sqoop-1.99.7-bin-hadoop200.tar.gz 2.解压 tar -z ...
- 36. CentOS-6.3安装Mysql集群
安装要求 安装环境:CentOS-6.3安装方式:源码编译安装 软件名称:mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz下载地址:http://mysql ...
- Eclipse安装STS(Spring Tool Suite (STS) for Eclipse)插件
转自:https://blog.csdn.net/zhen_6137/article/details/79383941
- mysql:rand()产生随机整数,CONCAT拼接时间字符串
用存储过程插入测试数据,如果不想update_time都一样,可以进行随机字符串拼接:2月随机1天,小时随机 CONCAT('2017-02-',FLOOR(1 + (RAND() * 28)),' ...

