方法一为下载一张图片
方法二为 下载两张图片拼接成一张
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self comBine];
}
-(void)downLoad
{
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
NSBlockOperation *op1=[NSBlockOperation blockOperationWithBlock:^{
NSURL *url=[NSURL URLWithString:@"https://gw.alicdn.com/imgextra/i1/1737953600/TB2th_0tVXXXXa5XpXXXXXXXXXX_!!1737953600.jpg_600x600"];
NSData *data=[NSData dataWithContentsOfURL:url];
UIImage *image=[UIImage imageWithData:data];
NSLog(@"image--------%@",[NSThread currentThread]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image=image;
NSLog(@"image--------%@",[NSThread currentThread]);
}];
}];
[queue addOperation:op1];
}
-(void)comBine
{
//创建队列
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
__block UIImage *image1;
__block UIImage *image2;
//下载图片1
NSBlockOperation *op1=[NSBlockOperation blockOperationWithBlock:^{
NSURL *url=[NSURL URLWithString:@"https://gw.alicdn.com/imgextra/i1/1737953600/TB2th_0tVXXXXa5XpXXXXXXXXXX_!!1737953600.jpg_600x600"];
NSData *data=[NSData dataWithContentsOfURL:url];
image1=[UIImage imageWithData:data];
NSLog(@"image--------%@",[NSThread currentThread]);
}];
//下载图片2
NSBlockOperation *op2=[NSBlockOperation blockOperationWithBlock:^{
NSURL *url=[NSURL URLWithString:@"https://gw.alicdn.com/imgextra/i1/1737953600/TB2th_0tVXXXXa5XpXXXXXXXXXX_!!1737953600.jpg_600x600"];
NSData *data=[NSData dataWithContentsOfURL:url];
image2=[UIImage imageWithData:data];
NSLog(@"image--------%@",[NSThread currentThread]);
}];
//合成图片
NSBlockOperation *comBine=[NSBlockOperation blockOperationWithBlock:^{
UIGraphicsBeginImageContext(CGSizeMake(300, 300));
[image1 drawInRect:CGRectMake(0, 0, 150, 300)];
[image2 drawInRect:CGRectMake(150, 0, 150, 300)];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndPDFContext();
//转到住线程
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
self.imageView.image=image;
NSLog(@"image--------%@",[NSThread currentThread]);
}];
}];
//设置依赖
[comBine addDependency:op1];
[comBine addDependency:op2];
//添加到队列
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:comBine];
}
@end