-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{[Nsthread detachnewthreadselector: @selector (download) totarget:self Withobject:nil];}#pragmaMark----------------------#pragmaMethods//Open sub-thread download picture, back to main thread refresh UI-(void) download{//1. Determine the URLNsurl *url = [Nsurl urlwithstring:@"Http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.700_0.jpeg"]; //2. Download picture binary data to local according to URLNSData *imagedata =[NSData Datawithcontentsofurl:url]; //3. Convert Picture formatUIImage *image =[UIImage Imagewithdata:imagedata]; NSLog (@"Download----%@", [Nsthread CurrentThread]); //4. Back to the main thread display UI /*first parameter: Which method to call to the main thread the second argument: the parameter that the preceding method needs to pass here is the third parameter of the image: whether to wait for*/ //[Self performselectoronmainthread: @selector (showImage:) withobject:image Waituntildone:no];[Self performselector: @selector (showImage:) onthread:[nsthread Mainthread] Withobject:image Waituntildone:yes] ; //self.imageView.image = image;NSLog (@"---End---");}//Update UI Actions-(void) ShowImage: (UIImage *) image{Self.imageView.image=image; NSLog (@"UI----%@", [Nsthread CurrentThread]);}
Performselectoronmainthread: And Performselector: Methods are available
You can also call the Performselectoronmainthread: Method directly with the object Self.imageview, using the setimage of the object Self.imageview's Image property: method to implement (this can omit the Create ShowImage method).
[Self.imageview performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:yes];
Open sub-thread download picture, back to main thread refresh UI step