- (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