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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现