//1. Queue Groupsdispatch_group_t Group =dispatch_group_create (); dispatch_queue_t Queue= Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //2. Download Picture 1__block UIImage *image1 =Nil; Dispatch_group_async (group, queue,^{Nsurl*URL1 = [Nsurl urlwithstring:@"http://111.jpg"]; NSData*data1 =[NSData DATAWITHCONTENTSOFURL:URL1]; Image1=[UIImage imagewithdata:data1]; }); //3. Download Picture 2__block UIImage *image2 =Nil; Dispatch_group_async (group, queue,^{Nsurl*URL2 = [Nsurl urlwithstring:@"Http://222.png"]; NSData*DATA2 =[NSData Datawithcontentsofurl:url2]; Image2=[UIImage Imagewithdata:data2]; }); //4. Merge pictures (ensure that all tasks within the group are executed, and then execute the block inside the Notify function)Dispatch_group_notify (group, queue, ^{ //To open a bitmap contextUigraphicsbeginimagecontextwithoptions (Image1.size, NO,0.0); //draw the 1th pictureCGFloat image1w =Image1.size.width; CGFloat image1h=Image1.size.height; [Image1 Drawinrect:cgrectmake (0,0, IMAGE1W, image1h)]; //draw the 2nd pictureCGFloat image2w = Image2.size.width *0.5; CGFloat image2h= Image2.size.height *0.5; CGFloat image2y= image1h-image2h; [Image2 Drawinrect:cgrectmake (0, image2y, image2w, image2h)]; //get the picture in contextUIImage *fullimage =Uigraphicsgetimagefromcurrentimagecontext (); //End ContextUigraphicsendimagecontext (); //5. Go back to the main thread to display the pictureDispatch_async (Dispatch_get_main_queue (), ^{self.imageView.image=Fullimage; }); });
IOS-GCD use of queue groups