dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);//執(zhí)行異步加載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(@"下載結(jié)束,開始組合,顯示要回到主線程");
//開啟圖形上下文
? ? ? ? ? ? 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();
?//關(guān)閉
? ? ? ? ? ? ? ?UIGraphicsEndImageContext();
? ? });
//回到主線程刷新UI
?dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? self.imageView.image?= image;
?NSLog(@"%@",[NSThread?currentThread]);
? ? ? ? });