-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1.创建队列
NSOperationQueue *queue= [[NSOperationQueue alloc]init]; NSBlockOperation *download1 = [NSBlockOperation blockOperationWithBlock:^{
//2.1.确定要下载网络图片的url地址,一个url唯一对应着网络上的一个资源
NSURL *url = [NSURL URLWithString:@"http://p6.qhimg.com/t01d2954e2799c461ab.jpg"]; //2.2.根据url地址下载图片数据到本地(二进制数据)
NSData *data = [NSData dataWithContentsOfURL:url]; //2.3.把下载到本地的二进制数据转换成图片
self.image1 = [UIImage imageWithData:data]; }]; NSBlockOperation *download2 = [NSBlockOperation blockOperationWithBlock:^{
//2.1.确定要下载网络图片的url地址,一个url唯一对应着网络上的一个资源
NSURL *url = [NSURL URLWithString:@"http://p6.qhimg.com/t01d2954e2799c461ab.jpg"]; //2.2.根据url地址下载图片数据到本地(二进制数据)
NSData *data = [NSData dataWithContentsOfURL:url]; //2.3.把下载到本地的二进制数据转换成图片
self.image2 = [UIImage imageWithData:data]; }]; NSBlockOperation *combie = [NSBlockOperation blockOperationWithBlock:^{ UIGraphicsBeginImageContext(CGSizeMake(, )); [self.image1 drawInRect:CGRectMake(, , , )];
[self.image2 drawInRect:CGRectMake(, , , )]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //刷新UI
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.imageView.image = image;
NSLog(@"%@----",[NSThread currentThread]);
}]; }]; [combie addDependency:download1];
[combie addDependency:download2]; [queue addOperation:download1];
[queue addOperation:download2];
[queue addOperation:combie];
}
示例