ios开发多线程篇——线程间的通信
2016-04-20 10:35
537 查看
一.简单说明
线程间通信:在一个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信。
线程间通信的体现:
一个线程传递数据给另一个线程。
在一个线程中执行完特定任务后,转到另一个线程继续执行任务。
线程间通信常用方法
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;
线程间通信示例-图片下载
代码一:
代码二
线程间通信:在一个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信。
线程间通信的体现:
一个线程传递数据给另一个线程。
在一个线程中执行完特定任务后,转到另一个线程继续执行任务。
线程间通信常用方法
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;
线程间通信示例-图片下载
代码一:
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong) UIImageView *iconView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.iconView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.view addSubview:self.iconView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)download{ //从网络中下载图片 NSURL *url = [NSURL URLWithString:@"http://banbao.chazidian.com/uploadfile/2016-01-25/s145368924044608.jpg"]; //把图片转换成二进制的数据 NSData *data = [NSData dataWithContentsOfURL:url]; //把数据转换成图片 UIImage *image = [UIImage imageWithData:data]; //回到主线程中设置图片 [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //在子线程中调用download方法下载图片 [self performSelectorInBackground:@selector(download) withObject:nil]; } - (void)settingImage:(UIImage *)image{ self.iconView.image = image; } @end
代码二
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong) UIImageView *iconView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.iconView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.view addSubview:self.iconView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)download:(NSString *)title{ NSLog(@"title=%@",title); //从网络中下载图片 NSURL *url = [NSURL URLWithString:@"http://banbao.chazidian.com/uploadfile/2016-01-25/s145368924044608.jpg"]; //把图片转换成二进制的数据 NSData *data = [NSData dataWithContentsOfURL:url]; //把数据转换成图片 UIImage *image = [UIImage imageWithData:data]; //回到主线程中设置图片 //第一种方式 // [self performSelectorOnMainThread:@selector(settingImage:) // withObject:image // waitUntilDone:NO]; //第二种方式 // [self.iconView performSelector:@selector(setImage:) // onThread:[NSThread mainThread] // withObject:image // waitUntilDone:NO]; //第三种方式 [self.iconView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //在子线程中调用download方法下载图片 [self performSelectorInBackground:@selector(download:) withObject:@"下载图片"]; } - (void)settingImage:(UIImage *)image{ self.iconView.image = image; } @end
相关文章推荐
- iOS 集成极光推送(一)
- xcode下载iOS开发文档
- iOS-国家代码选择功能github开源分享
- iOS 微博第三方登录的简单实现
- 30分钟搞定iOS自定义相机
- iOS 处理第三方库冲突
- ios开发多线程篇——线程安全
- IOS开发之——自定义导航控制器
- iOS NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- 欢迎使用CSDN-markdown编辑器
- iOS 直播技术平台
- 关于键盘上方创建返回按钮
- 极验geetest IOS端开发文档
- iOS 处理图片的一些小 Tip
- 初识 iOS 9 中新的联系人框架
- iOS 开发多线程及其进程和线程的介绍和理解
- iOS 类别和扩展(Categories和Extensions)
- iOS中的小知识点
- iOS 数据缓存
- iOS选中文字点击learn崩溃