IOSGCD Communication in a thread

Source: Internet
Author: User

Today let's look at how the iOS thread communicates.

#import "ViewController.h"


@interface viewcontroller ()

{

uiimageview *_image;

}

@end


@implementation Viewcontroller


-(void) viewdidload {

[superviewdidload];


cgrect frame = [[uiscreen mainscreen] bounds ];

_image = [[uiimageviewalloc]init];

_image. Frame = CGRectMake (0,0, frame. ) size . width , frame. size . Height - - );

[self. View addsubview:_image];

}

// when the user touches the screen, open a thread, download the picture

-(void) Touchesbegan: ( Nsset *) touches withevent: (uievent *) Event

{

[self performselectorinbackground: @selector ( Downloadimage)withobject:nil];

}

// sub-thread tasks, responsible for downloading picture resources in the network

-(void) downloadimage

{

//1. constructs a picture resource URL

nsurl *url = [nsurlurlwithstring:@ "/http/ C.hiphotos.baidu.com/image/pic/item/e61190ef76c6a7ef2469025dfefaaf51f3de667a.jpg "];

//2. Download Image

nsdata *data = [nsdatadatawithcontentsofurl : url];

//3. the binary Data Convert to Picture object

UIImage *image = [UIImageimagewithdata:d ATA];

// in iOS development, The action to update the UI must be put down in the main thread to execute

[_imageperformselectoronmainthread:@selector( SetImage:)withobject: Image waituntildone:NO];

    

}


// give the downloaded image to Uiimageview

-(void) setimage: ( UIImage *) Image

{ _image. Image = image;

}

@end




This is the way of communication in the thread, when the user touches this screen, it opens a thread to perform the download task, the download completes, takes the main thread to download the good picture to Uiimageview.

IOSGCD Communication in a thread

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.