Communication between IOS threads (GCD)

Source: Internet
Author: User

1. Download a picture from the Web to display it on the view

- (void) imagedownload{Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{        //the network path of the pictureNsurl *url = [Nsurl urlwithstring:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"]; //Loading PicturesNSData *data =[NSData Datawithcontentsofurl:url]; //Create a pictureUIImage *image =[UIImage Imagewithdata:data]; //back to main threadDispatch_async (Dispatch_get_main_queue (), ^{self.imageView.image=image;    }); });}

2, download two pictures from the network, and so they both download finished, will they spell a picture.

@interface*ImageView; /* * * *image1;    /* * * *image2;    @end

- (void) group{dispatch_queue_t queue= Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //Create a queue groupdispatch_group_t Group =dispatch_group_create (); //1. Download Picture 1Dispatch_group_async (group, queue, ^{        //the network path of the pictureNsurl *url = [Nsurl urlwithstring:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"]; //Loading PicturesNSData *data =[NSData Datawithcontentsofurl:url]; //Create a pictureSelf.image1 =[UIImage Imagewithdata:data];        }); //2. Download Picture 2Dispatch_group_async (group, queue, ^{        //the network path of the pictureNsurl *url = [Nsurl urlwithstring:@"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"]; //Loading PicturesNSData *data =[NSData Datawithcontentsofurl:url]; //Create a pictureSelf.image2 =[UIImage Imagewithdata:data];        }); //3. Compose a new picture of picture 1, picture 2Dispatch_group_notify (group, queue, ^{        //to open a new graphics contextUigraphicsbeginimagecontext (Cgsizemake ( -, -)); //Drawing Pictures[Self.image1 Drawinrect:cgrectmake (0,0, -, -)]; [Self.image2 Drawinrect:cgrectmake ( -,0, -, -)]; //get the picture in contextUIImage *image =Uigraphicsgetimagefromcurrentimagecontext (); //End ContextUigraphicsendimagecontext (); //back to main thread display pictureDispatch_async (Dispatch_get_main_queue (), ^{            //4. Display the new pictureSelf.imageView.image =image;    }); });}

Communication between IOS threads (GCD)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.