Code for communication between threads

Source: Internet
Author: User

#import "ViewController.h"

@interface viewcontroller ()

picture frame Uiimageview

@property (weak, nonatomic) iboutlet uiimageview *imageview;

@end

@implementation Viewcontroller

-(void) viewdidload {

[Super viewdidload];

additional setup after loading the view, typically from a nib.

}

Create a sub-thread to perform the download of this time-consuming action click Method

-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event

{

the picture to download

nsstring *urlstring = @ "http://p1.yokacdn.com/pic/star/topic/2011/ U288p1t117d414828f2577dt20110902111814.jpg ";

//1. in sub-threading slices

nsthread *thread = [[nsthread alloc] initwithtarget:self selector :@selector(downloadwebimage:) object: urlstring];

put it in the callable thread pool .

[Thread start];

}

Download the web image Download method

-(void) Downloadwebimage: (nsstring *) urlstring

{

NSLog(@ "downloadwebimagebegan:%@", [nsthread currentthread]);

// Uniform Resource Locator 1> the address of the image to be downloaded to the Uniform Resource Locator

nsurl *url = [nsurl urlwithstring: urlstring];

// download image data Contents content download 2> convert a Uniform Resource Locator to data /c11>

nsdata *data = [nsdata datawithcontentsofurl: url];

// convert picture 3> data to Image

UIImage *image = [UIImage imagewithdata:d ATA];

//2. display pictures on the main thread

//[Self Performselectoronmainthread: @selector (setupimage:) withobject:image Waituntildone:yes];

when a picture is downloaded from a child thread , It is displayed in the main thread

[self performselector:@selector(setupimage:) onthread: [nsthread mainthread] withobject: Image Waituntildone: NO];

Print Current thread

NSLog(@ "downloadwebimageend:%@", [nsthread currentthread]);

}

set up a picture

-(void) Setupimage: (UIImage *) image

{

NSLog(@ "setupimage:%@", [nsthread currentthread]);

// show Pictures

self. ImageView. Image = image;

}

@end

Code for communication between threads

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.