@property (weak,nonatomic) Iboutletuilabel *downlabelinfo;
@property (weak,nonatomic) Iboutletuiimageview *imageview;
@end
@implementationViewController
-(void) viewdidload
{
[Super Viewdidload];
NSString *url [email protected] "http://d.hiphotos.baidu.com/image/w%3D1366%3Bcrop%3D0%2C0%2C1366%2C768/sign= 6cbcab9dabec8a13141a53e3c135aaec/aa64034f78f0f7369453c3730855b319ebc41316.jpg ";
@autoreleasepool {
Nsthread *thread = [[Nsthreadalloc]initwithtarget:selfselector: @selector (downloadimage:) Object:url];
[Thread start];
}
}
-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
}
-(void) Downloadimage: (NSString *) URL {
To put the displayed content in front of the synchronization method
Self->_downlabelinfo.text [email protected] "Downloading pictures ...";
The synchronization method will card the thread until the completion position
NSData *imagedata = [Nsdatadatawithcontentsofurl:[nsurlurlwithstring:url]];
UIImage *image =[uiimageimagewithdata:imagedata];
if (Image==nil)
{
}
Else
{
Self->_downlabelinfo.text = @ "Downloading pictures ..."; so it's useless to write this statement.
[Self Performselectoronmainthread: @selector (updateimageview:) Withobject: (image) Waituntildone:yes];
}
}
-(void) Updateimageview: (UIImage *) image
{
Self->_imageview.image = image;
Self->_imageview.frame=cgrectmake (self.view.frame.size.width/2-100,self.view.frame.size.height/2-100,200, 200);
Self->_downlabelinfo.textcolor = [Uicolorredcolor];
Self->_downlabelinfo.text [email protected] "Picture Download complete";
}
@end
The second way is to use nsurlconnection sendsynchronousrequest sync to get the picture content and display
Modify the Downloadimage method as follows
-(void) Downloadimage: (NSString *) URL
{
Self->_downlabelinfo.text [email protected] "Downloading pictures ...";
Nsurlrequest *request = [Nsurlrequestrequestwithurl:[nsurlurlwithstring:url]];
Nserror *error;
This will cause blocking
NSData *data = [nsurlconnectionsendsynchronousrequest:requestreturningresponse:nilerror:&error];
if (data ==nil)
{
NSLog (@ "nil");
}
Else
{
UIImage *image =[uiimageimagewithdata:data];
Self->_imageview.image = image;
Self->_downlabelinfo.textcolor = [Uicolorredcolor];
Self->_downlabelinfo.text [email protected] "Picture Download complete";
}
}