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