#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