Communication between IOS----threads

Source: Internet
Author: User

When the number of threads is greater than one, there may be communication between threads, and results from one thread will be used by another.

For example, the loading of commonly used images is this way. The picture is loaded in a sub-thread, and when the picture is loaded, it will go back to the main thread to refresh the UI and display the image.

#import"ViewController.h"@interface Viewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview*IconView, @end @implementation viewcontroller- (void) viewdidload{[Super Viewdidload];}-(void) Touchesbegan: (nsset*) touches withevent: (Uievent *)Event{[Self performselectorinbackground: @selector (download) withobject:nil];}-(void) download{//1. Download the picture according to the URL//download pictures from the networkNsurl *urlstr = [Nsurl urlwithstring:@"asdf"]; //convert a picture to binary dataNSData *data =[NSData Datawithcontentsofurl:urlstr]; //convert the data into picturesUIImage *image =[UIImage Imagewithdata:data]; //2. Go back to the main thread to set the picture[Self performselectoronmainthread: @selector (settingimage:) withobject:image Waituntildone:no];

  The second way
    [Self.imageview performselector: @selector (setimage:) onthread:[nsthread Mainthread] Withobject:image Waituntildone:no];      

The Third Way : Picture call set method, very good!!!

[Self.iconview performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:no];



// Set Display Picture -(void) Settingimage: (UIImage *) image {Self.iconView.image=-( void ) didreceivememorywarning {[Super didreceivememorywarning];}
@end

This article refers to the top-of-the-text blog:

Http://www.cnblogs.com/wendingding/p/3805884.html

Communication between IOS----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.