标签:oid dde 获取 opera queue interface raw 属性 sign
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 /** 6 图片 7 */ 8 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 9 10 @end 11 12 @implementation ViewController 13 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 14 // [self downloadImage]; 15 [self downloadImagAndCompoundIma]; 16 } 17 18 19 /** 20 下载图片 并且合成
21 */ 22 -(void)downloadImagAndCompoundIma{ 23 24 /* 25 获取图片属性1:搞两个属性 然后self.img1, self.img2 26 获取图片属性2: 使用__blcok修饰, 27 */ 28 __block UIImage *ima1 = [[UIImage alloc] init]; 29 __block UIImage *ima2 = [[UIImage alloc] init]; 30 31 //1.创建 非主队列 32 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 33 34 //2.创建任务 : 下载图片1 35 NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{ 36 37 NSString *urlStr = [NSString stringWithFormat:@"http://pic.90sjimg.com/design/00/07/85/23/58ef8faf71ffe.png"]; 38 NSURL *url = [NSURL URLWithString:urlStr]; 39 NSData *data = [NSData dataWithContentsOfURL:url]; 40 ima1 = [UIImage imageWithData:data]; 41 }]; 42 43 //3.创建任务 : 下载图片2 44 NSBlockOperation * op2 = [NSBlockOperation blockOperationWithBlock:^{ 45 // 下载图片 46 NSString *urlStr = [NSString stringWithFormat:@"http://pic28.nipic.com/20130330/9607253_143631959000_2.png"]; 47 NSURL *url = [NSURL URLWithString:urlStr]; 48 NSData *data = [NSData dataWithContentsOfURL:url]; 49 ima2 = [UIImage imageWithData:data]; 50 }]; 51 52 //4.合成图片 53 NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{ 54 //1。开启图形上下文 并且设置上下文 宽高 55 UIGraphicsBeginImageContext(CGSizeMake(200, 200)); 56 57 //2.图片画图 58 [ima1 drawInRect:CGRectMake(0, 0, 100, 200)]; 59 ima1 = nil; 60 61 [ima2 drawInRect:CGRectMake(100, 0, 100, 200)]; 62 ima2 = nil; 63 64 //3.根据图形上下文去图片 65 UIImage *ima = UIGraphicsGetImageFromCurrentImageContext(); 66 67 //4.关闭上下文 68 UIGraphicsEndImageContext(); 69 //3.回到主线程刷新UI 70 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 71 self.imageView.image = ima; 72 }]; 73 }]; 74 75 //5.添加依赖,因为全都是异步执行 谁先谁后不可控 76 [op3 addDependency:op1]; 77 [op3 addDependency:op2]; 78 79 //3.添加任务 80 [queue addOperation:op1]; 81 [queue addOperation:op2]; 82 [queue addOperation:op3]; 83 } 84 85
86 /** 87 下载图片
88 */ 89 -(void)downloadImage{ 90 91 //1.创建 非主队列 92 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 93 94 //2.创建任务 95 NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{ 96 97 //2.1 下载图片 98 NSString *urlStr = [NSString stringWithFormat:@"http://pic.90sjimg.com/design/00/07/85/23/58ef8faf71ffe.png"]; 99 NSURL *url = [NSURL URLWithString:urlStr]; 100 NSData *data = [NSData dataWithContentsOfURL:url]; 101 UIImage *ima = [UIImage imageWithData:data]; 102 103 //3.回到主线程刷新UI 104 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 105 self.imageView.image = ima; 106 }]; 107 108 }]; 109 110 //3.添加任务 111 [queue addOperation:op1]; 112 } 113 @end
OC 线程操作3 - NSOperation 实现线程间通信
标签:oid dde 获取 opera queue interface raw 属性 sign
原文地址:https://www.cnblogs.com/qingzZ/p/9262639.html