iOS開發(fā)- gcd實(shí)現(xiàn)同步若干異步加載多張圖片然后合成顯示

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]);

? ? ? ? });

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容