iOS開發多線程篇—線程間的通訊

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   color   

iOS開發多線程篇—線程間的通訊

 

一、簡單說明

線程間通訊:在1個進程中,線程往往不是孤立存在的,多個線程之間需要經常進行通訊

 

線程間通訊的體現

1個線程傳遞資料給另1個線程

在1個線程中執行完特定任務後,轉到另1個線程繼續執行任務

 

線程間通訊常用方法

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;

 

線程間通訊樣本 – 圖片下載

代碼1:

 1 // 2 //  YYViewController.m 3 //  06-NSThread04-線程間通訊 4 // 5 //  Created by apple on 14-6-23. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 @interface YYViewController ()11 @property (weak, nonatomic) IBOutlet UIImageView *iconView;12 @end13 14 @implementation YYViewController15 16 - (void)viewDidLoad17 {18     [super viewDidLoad];19 }20 21 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event22 {23 24 // 在子線程中調用download方法下載圖片25     [self performSelectorInBackground:@selector(download) withObject:nil];26 }27 28  29 30 -(void)download31 {32     //1.根據URL下載圖片33     //從網路中下載圖片34     NSURL *urlstr=[NSURL URLWithString:@"fdsf"];35 36     //把圖片轉換為二進位的資料37     NSData *data=[NSData dataWithContentsOfURL:urlstr];//這一行操作會比較耗時38 39     //把資料轉換成圖片40     UIImage *image=[UIImage imageWithData:data];41  42     //2.回到主線程中設定圖片43     [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO];44 }45 46  47 48 //設定顯示圖片49 -(void)settingImage:(UIImage *)image50 {51     self.iconView.image=image;52 }53 54 @end

 

代碼2:

 1 // 2 //  YYViewController.m 3 //  06-NSThread04-線程間通訊 4 // 5 //  Created by apple on 14-6-23. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import <NSData.h>11 12 @interface YYViewController ()13 @property (weak, nonatomic) IBOutlet UIImageView *iconView;14 @end15 16 @implementation YYViewController17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21 }22 23  24 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event25 {26 // 在子線程中調用download方法下載圖片27 28     [self performSelectorInBackground:@selector(download) withObject:nil];29 }30 31  32 -(void)download33 {34 35     //1.根據URL下載圖片36     //從網路中下載圖片37     NSURL *urlstr=[NSURL URLWithString:@"fdsf"];38 39     //把圖片轉換為二進位的資料40     NSData *data=[NSData dataWithContentsOfURL:urlstr];//這一行操作會比較耗時41 42     //把資料轉換成圖片43     UIImage *image=[UIImage imageWithData:data];44 45     //2.回到主線程中設定圖片46     //第一種方式47 //    [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO];48 49     //第二種方式50     //    [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];51 52     //第三種方式53    [self.iconView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];54 }55 56 57 //設定顯示圖片58 //-(void)settingImage:(UIImage *)image59 //{60 //    self.iconView.image=image;61 //}62 63 @end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.