iOS Development small feature: communication between threads (3 ways)

Source: Internet
Author: User

Three methods are achieved through the touch of the Touchesbegin monitor screen

First, Performselector Way

1 #import "ViewController.h"2 @interfaceViewcontroller ()3@property (Weak, nonatomic) Iboutlet Uiimageview *ImageView;4 @end5 @implementationViewcontroller6- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event7 {8     //Put child Threads9 [Self performselectorinbackground: @selector (DOWNLOAD3) Withobject:nil];Ten } One  A //download into the sub-thread, the display picture should be placed in the main thread!!! Doing so will cause a refresh problem -- (void) Download3 - { the     //the network path of the picture -Nsurl *url = [Nsurl urlwithstring:@"http://ww2.sinaimg.cn/mw690/63e6fd01jw1f3f3rf75goj20qo0zkagy.jpg"]; -     //Download Image Data -NSData *data =[NSData Datawithcontentsofurl:url]; +      -     //Create a picture +UIImage *image =[UIImage Imagewithdata:data]; A     //back to the main thread show Picture method one: at //[Self performselectoronmainthread: @selector (showImage:) withobject:image Waituntildone:yes]; -     //back to main thread show Picture method two: -     //Waituntildone: Indicates whether to wait for the main thread to finish and go down, yes means to do the following when done, no means to execute with the following things - [Self.imageview performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:yes]; -     //back to the main thread show Picture method Three: - [Self.imageview performselector: @selector (setimage:) onthread:[nsthread Mainthread] Withobject:image Waituntildone:yes]; in } - //main thread Display picture to- (void) ShowImage: (UIImage *) Image + { -Self.imageView.image =image; the}

Second, the GCD way

1- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event2 {3Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{4         //the network way of the picture5Nsurl *url = [Nsurl urlwithstring:@"http://ww2.sinaimg.cn/mw1024/75614297jw1f34e5llyz4j20qo0zj0zl.jpg"];6         //Loading Pictures7NSData *data =[NSData Datawithcontentsofurl:url];8         //Create a picture9UIImage *image =[UIImage Imagewithdata:data];Ten         //back to main thread OneDispatch_async (Dispatch_get_main_queue (), ^{ ASelf.imageView.image =image; -         }); -     }); the      -}

Third, Operation Way (this way more object-oriented features!) )

1- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event2 {3     //start a child thread directly to perform a task4[[[Nsoperationqueue alloc] init] addoperationwithblock:^{5Nsurl *url = [Nsurl urlwithstring:@"http://ww4.sinaimg.cn/mw690/63e6fd01jw1ezxz499hy5j21gt0z94qq.jpg"];6NSData *data =[NSData Datawithcontentsofurl:url];7UIImage *image =[UIImage Imagewithdata:data];8         //back to main thread9[[Nsoperationqueue Mainqueue] addoperationwithblock:^{Ten             //Show Pictures OneSelf.imageView.image =image; A         }]; -     }]; -}

The above three ways all need to drag a imageview in main storyboard, then set the automatic cloth!!

iOS Development small feature: communication between threads (3 ways)

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.