Open sub-thread download picture, back to main thread refresh UI step

Source: Internet
Author: User

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

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.