异步下载多张图片, 合并成一张显示
//开启子线程
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"%@",[NSThread currentThread]);
//下载地址
NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
self.image1 = [UIImage imageWithData:data1];
NSURL *url2 = [NSURL URLWithString:@"http://scimg.jb51.net/allimg/160716/105-160G61F250436.jpg"];
NSData *data2 = [NSData dataWithContentsOfURL:url2];
self.image2 = [UIImage imageWithData:data2];
//开启图形上下文
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]);
});
});