//1.开子线程下载图片
//1.1非主队列
NSOperationQueue*queue = [[NSOperationQueuealloc]init];
//1.2封装操作
NSBlockOperation*download = [NSBlockOperationblockOperationWithBlock:^{
NSURL*url = [NSURLURLWithString:@"http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e"];
NSData*imageData = [NSDatadataWithContentsOfURL:url];
UIImage*image = [UIImageimageWithData:imageData];
NSLog(@"download---%@",[NSThreadcurrentThread]);
//3.更新UI
[[NSOperationQueuemainQueue]addOperationWithBlock:^{
self.imageView.image= image;
NSLog(@"UI---%@",[NSThreadcurrentThread]);
}];
}];
//2.添加操作到队列
[queueaddOperation:download];
-(void)comBie
{
//1.创建队列
NSOperationQueue*queue = [[NSOperationQueuealloc]init];
__blockUIImage*image1;
__blockUIImage*image2;
//2封装操作,下载图片1
NSBlockOperation*download1 = [NSBlockOperationblockOperationWithBlock:^{
NSURL*url = [NSURLURLWithString:@"http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e"];
NSData*imageData = [NSDatadataWithContentsOfURL:url];
image1 = [UIImageimageWithData:imageData];
NSLog(@"download---%@",[NSThreadcurrentThread]);
}];
//3封装操作,下载图片2
NSBlockOperation*download2 = [NSBlockOperationblockOperationWithBlock:^{
NSURL*url = [NSURLURLWithString:@"http://www.027art.com/feizhuliu/UploadFiles_6650/201109/2011091718442835.jpg"];
NSData*imageData = [NSDatadataWithContentsOfURL:url];
image2 = [UIImageimageWithData:imageData];
NSLog(@"download---%@",[NSThreadcurrentThread]);
}];
//4.封装合并图片的操作
NSBlockOperation*combie = [NSBlockOperationblockOperationWithBlock:^{
//4.1开上下文
UIGraphicsBeginImageContext(CGSizeMake(200,200));
//4.2画图1
[image1drawInRect:CGRectMake(0,0,100,200)];
//4.3画图2
[image2drawInRect:CGRectMake(100,0,100,200)];
//4.4根据上下文得到图片
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
//4.5关闭上下文
UIGraphicsEndImageContext();
//7.更新UI
[[NSOperationQueuemainQueue]addOperationWithBlock:^{
self.imageView.image= image;
NSLog(@"UI----%@",[NSThreadcurrentThread]);
}];
}];
//5.设置依赖关系
[combieaddDependency:download1];
[combieaddDependency:download2];
//6.添加操作到队列中
[queueaddOperation:download2];
[queueaddOperation:download1];
[queueaddOperation:combie];
}