Simple use of iOS development GCD

Source: Internet
Author: User
Tags gcd

- (void) viewdidload {[Super viewdidload]; //GCD can fully invoke the device's CPU to maximize performance, encapsulated on the C language basis//dispatch_queue_t opens up a thread//Dispatch_queue_priority_default Setting the priority leveldispatch_queue_t Queue= Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //On This line thread open the asynchronous downloadDispatch_async (Queue, ^{UIImage* image = [self Imagewithstringurl:@"http://www.chinadaily.com.cn/hqgj/jryw/201205/f5aae8120f566b9cca8cf29791f111fd.jpg"]; Dispatch_async (Dispatch_get_main_queue (),^{//set the contents of the UI on the main thread, and move the contents of the UI to the main line threadUiimageview* View =[[Uiimageview alloc] initWithFrame:self.view.frame]; View.image=image;                    [Self.view Addsubview:view];    });        }); [Self Creategcdtimer];}-(void) creategcdtimer{//dispatch_source_t is an object that monitors certain types of events, and when these events occur, he automatically puts a block into the Dispatch_get_queue execution queuedispatch_source_t timer = dispatch_source_create (Dispatch_source_type_timer,0,0, Dispatch_get_main_queue ());//Set the timer's properties to be triggered every 10 seconds with an error in the 1-second rangeDispatch_source_set_timer (Timer, dispatch_walltime (NULL,0),0.2*nsec_per_sec,1*nsec_per_sec); Dispatch_source_set_event_handler (Timer,^{//write the contents of the timer processing here        Static inti =0; NSLog (@"%d", i); I++;//must be structurally complete        if(i> -) {dispatch_source_cancel (timer); }    });//Start TimerDispatch_resume (timer);}-(UIImage *) Imagewithstringurl: (NSString *) str{Nsurl* URL =[Nsurl Urlwithstring:str]; NSData* data =[NSData Datawithcontentsofurl:url]; UIImage* Image =[UIImage Imagewithdata:data]; returnimage;}

Simple use of iOS development 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.