dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);//执行异步加载dispatch_group_async(group, defaultQueue, ^{
NSLog(@"异步下载1");
NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
self.image1 = [UIImage imageWithData:data1];
});
dispatch_group_async(group, defaultQueue, ^{
NSLog(@"异步下载2");
NSURL *url2= [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
NSData *data2 = [NSData dataWithContentsOfURL:url1];
self.image2 = [UIImage imageWithData:data1];
});
dispatch_group_async(group, defaultQueue, ^{
NSLog(@"异步下载3");
});
dispatch_group_async(group, defaultQueue, ^{
NSLog(@"异步下载4");
});
//组合图片
dispatch_group_notify(group, defaultQueue, ^{
NSLog(@"下载结束,开始组合,显示要回到主线程");
//开启图形上下文
UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, NO, 0.0);
CGFloat image1Width = self.imageView.frame.size.width;
CGFloat image1Height = self.imageView.frame.size.height*0.5;
//绘制
[self.image1 drawInRect:CGRectMake(0, 0, image1Width, image1Height)];
CGFloat image2Width = self.imageView.frame.size.width;
CGFloat image2Height = self.image2.size.height*0.5;
CGFloat image2Y = self.imageView.frame.size.height - image1Height;
[self.image2 drawInRect:CGRectMake(0, image2Y, image2Width, image2Height)];
//取出图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//关闭
UIGraphicsEndImageContext();
});
//回到主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
NSLog(@"%@",[NSThread currentThread]);
});