iOS----線程之間的通訊

來源:互聯網
上載者:User

標籤:www   顯示圖片   url   obj   htm   color   UI   void   until   

當線程的數量大於一個的時候,線程之間可能會產生通訊,既一個線程產生的結果要被另一個線程用到。

比如常用的圖片的載入就是這個樣子。圖片的載入是在子線程進行的,當圖片載入完畢,就會回到主線程中重新整理UI,展示圖片。

#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.根據URL下載圖片    //從網路中下載圖片    NSURL *urlStr = [NSURL URLWithString:@"asdf"];     //把圖片轉換為二進位的資料    NSData *data = [NSData dataWithContentsOfURL:urlStr];    //把資料轉換成圖片    UIImage *image = [UIImage imageWithData:data];    //2.回到主線程中設定圖片    [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO];

    //第二種方式

      //  [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];      

  //第三種方式 :圖片調用set方法,很好!!!

    // [self.iconView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
} 


//設定顯示圖片 -(void)settingImage:(UIImage *)image { self.iconView.image=image; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
@end

 

本文參考文頂頂的部落格:

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

 

iOS----線程之間的通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.