IOS development features 11: Inter-thread communication (3 methods) and ios3

Source: Internet
Author: User

IOS development features 11: Inter-thread communication (3 methods) and ios3

All three methods are implemented by monitoring the touch of the screen through touchesBegin.

1. performSelector Mode

1 # import "ViewController. h "2 @ interface ViewController () 3 @ property (weak, nonatomic) IBOutlet UIImageView * imageView; 4 @ end 5 @ implementation ViewController 6-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event 7 {8 // put it into sub-thread 9 [self processing mselectorinbackground: @ selector (download3) withObject: nil]; 10} 11 12 // download and put the image into the subthread. The displayed image should be placed in the main thread !!! Otherwise refresh issues 13-(void) download314 {15 // The picture's network path 16 NSURL * url = [NSURL URLWithString: @ "http://ww2.sinaimg.cn/mw690/63e6fd01jw1f3f3rf75goj20qo0zkagy.jpg"]; 17 // download image data 18 NSData * data = [NSData dataWithContentsOfURL: url]; 19 20 // generate image 21 UIImage * image = [UIImage imageWithData: data]; 22 // return to the main thread for image Display Method 1: 23 // [self generated mselecw.mainthread: @ selector (showImage :) withObject: image waitUntilDone: YES]; 24 // return to the main thread to display the Image Method 2: 25 // waitUntilDone: Indicates whether to wait for the main thread to finish the task and go down. YES indicates that the following task is executed after the task is finished, NO indicates executing 26 [self. imageView example mselecw.mainthread: @ selector (setImage :) withObject: image waitUntilDone: YES]; 27 // method 3: 28 [self. imageView initialize mselector: @ selector (setImage :) onThread: [NSThread mainThread] withObject: image waitUntilDone: YES]; 29} 30 // The main thread displays the picture 31-(void) showImage :( UIImage *) image32 {33 self. imageView. image = image; 34}

 

Ii. GCD Mode

1-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event 2 {3 dispatch_async (dispatch_get_global_queue (queue, 0 ), ^ {4 // picture network path 5 NSURL * url = [NSURL URLWithString: @ "http://ww2.sinaimg.cn/mw1024/75614297jw1f34e5llyz4j20qo0zj0zl.jpg"]; 6 // load Picture 7 NSData * data = [NSData dataWithContentsOfURL: url]; 8 // generate image 9 UIImage * image = [UIImage imageWithData: data]; \ 10 // return to main thread 11 dispatch_async (dispatch_get_main_queue (), ^ {12 self. imageView. image = image; 13}); 14}); 15 16}

 

Iii. operation (this method is more object-oriented !)

 

1-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event 2 {3 // directly start the subthread to execute the Task 4 [[[NSOperationQueue alloc] init] addOperationWithBlock: ^ {5 NSURL * url = [NSURL URLWithString: @ "http://ww4.sinaimg.cn/mw690/63e6fd01jw1ezxz499hy5j21gt0z94qq.jpg"]; 6 NSData * data = [NSData dataWithContentsOfURL: url]; 7 UIImage * image = [UIImage imageWithData: data]; 8 // return to the main thread 9 [[NSOperationQueue mainQueue] addOperationWithBlock: ^ {10 // display image 11 self. imageView. image = image; 12}]; 13}]; 14}

 

You must drag an imageView to the main storyboard and set the automatic layout !!

Related Article

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.