//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.創(chuàng)建隊列
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根據(jù)上下文得到圖片
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
//4.5關(guān)閉上下文
UIGraphicsEndImageContext();
//7.更新UI
[[NSOperationQueuemainQueue]addOperationWithBlock:^{
self.imageView.image= image;
NSLog(@"UI----%@",[NSThreadcurrentThread]);
}];
}];
//5.設(shè)置依賴關(guān)系
[combieaddDependency:download1];
[combieaddDependency:download2];
//6.添加操作到隊列中
[queueaddOperation:download2];
[queueaddOperation:download1];
[queueaddOperation:combie];
}