IOS UI multithreaded Nsthread download and display pictures to Uiimageview

Source: Internet
Author: User

@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";

}

}

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.