iOS线程间通信
2016-04-19 09:19
204 查看
//原子和非原子属性
//oc在定义属性nonatomic和atomic两种选择
//atomic:原子属性
每调一回set方法,为set方法加锁
设值之前加把锁,设完值,锁打开。。。防止多线程访问值出现混乱状态
//nonatomic 非原子属性,不会为setter方法加锁
//atomic:线程安全,需要消耗大量的资源
//nonatomic 非线程安全,适合内存小的移动设备
//ios开发的建议:所有属性都声明为nonatomic
//尽量避免多线程抢夺统一资源
//尽量将加锁,资源抢夺的业务逻辑交给服务器端处理,减少移动客户端的压力
//线程间通信
//在一个进程中,线程往往不是独立存在的,多个线程之间需要经常进行通信
//线程间通信的体现
//1个线程传递数据为另外一个线程
//在一个线程中执行完特定任务后,转到另外一个线程继续执行任务
//线程间通信示列
--
图片下载
//个人感觉重点
//主线程
添加UIImageView
//在主线程添加UIImageView
为什么:主线程是UI线程,显示控件,刷新UI控件内容等等
//子线程
:在子线程下载图片
在子线程下载完图片,下载完毕
//回到
在主线程
显示图片
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent
*)event{
// [self download2];
[self
performSelectorInBackground:@selector(download3)
withObject:nil];
}
- (void)download1{
//图片下载原理
self.imageV = [UIImageView
new];
self.imageV.frame =
CGRectMake(0,
100, 100,
100);
[self.view
addSubview:self.imageV];
// self.imageV.image =
[UIImage imageName:这个是项目里的图片]
//下载网络图片用NSData
二进制数据0101...那种数据
//一个URL代表一个路径
不管是本地路径,远程路径都通过URL
//图片的网络路径
NSURL *url = [NSURL
URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
//计算时间方法一
NSDate *begin = [NSDate
date];
//根据图片的网络路径去下载图片数据
NSData *data = [NSData
dataWithContentsOfURL:url];
//显示图片
NSDate *end = [NSDate
date];
//计算时间间隔
NSLog(@"%f",[end
timeIntervalSinceDate:begin]);
self.imageV.image = [UIImage
imageWithData:data];
}
//计算时间方法二
- (void)download2{
self.imageV = [UIImageView
new];
self.imageV.frame =
CGRectMake(0,
100, 100,
100);
[self.view
addSubview:self.imageV];
//self.imageV.backgroundColor = [UIColor redColor];
//self.imageV.image
=
[UIImage imageName:这个是项目里的图片]
//下载网络图片用NSData
二进制数据0101...那种数据
//一个URL代表一个路径
不管是本地路径,远程路径都通过URL
//图片的网络路径
NSURL *url = [NSURL
URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
//计算时间间隔
CFTimeInterval begin =
CFAbsoluteTimeGetCurrent();
//根据图片的网络路径去下载图片数据
NSData *data = [NSData
dataWithContentsOfURL:url];
//显示图片
//计算时间间隔
CFTimeInterval end =
CFAbsoluteTimeGetCurrent();
//计算时间间隔
NSLog(@"%f",end -begin);
self.imageV.image = [UIImage
imageWithData:data];
}
- (void)download3{
//图片的网络路径
NSURL *url = [NSURL
URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
//加载图片
NSData *data = [NSData
dataWithContentsOfURL:url];
// 生成图片
UIImage *image = [UIImage
imageWithData:data];
//下载完毕要回到主线程,显示图片
//[self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
//回到主线程也可以
[self.imageV
performSelectorOnMainThread:@selector(setImage:)
withObject:nil
waitUntilDone:YES];
//waitUntilDone:YES];等这个走完,才继续走 no就是不等,一起走
// 回到主线程方法
// self.imageV performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#> modes:<#(nullable NSArray<NSString *> *)#>
// 回到主线程方法
// [self.imageV performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];
}
- (void)showImage:(UIImage *)image{
self.imageV.image = image;
// 打印线程,也可以这么打印
NSLog(@"%d",[NSThread
isMainThread]);
//线程间通信还有
不怎么用面试可以搭一答
// NSPort
// NSMessagePort
// NSMachPort
}
//oc在定义属性nonatomic和atomic两种选择
//atomic:原子属性
每调一回set方法,为set方法加锁
设值之前加把锁,设完值,锁打开。。。防止多线程访问值出现混乱状态
//nonatomic 非原子属性,不会为setter方法加锁
//atomic:线程安全,需要消耗大量的资源
//nonatomic 非线程安全,适合内存小的移动设备
//ios开发的建议:所有属性都声明为nonatomic
//尽量避免多线程抢夺统一资源
//尽量将加锁,资源抢夺的业务逻辑交给服务器端处理,减少移动客户端的压力
//线程间通信
//在一个进程中,线程往往不是独立存在的,多个线程之间需要经常进行通信
//线程间通信的体现
//1个线程传递数据为另外一个线程
//在一个线程中执行完特定任务后,转到另外一个线程继续执行任务
//线程间通信示列
--
图片下载
//个人感觉重点
//主线程
添加UIImageView
//在主线程添加UIImageView
为什么:主线程是UI线程,显示控件,刷新UI控件内容等等
//子线程
:在子线程下载图片
在子线程下载完图片,下载完毕
//回到
在主线程
显示图片
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent
*)event{
// [self download2];
[self
performSelectorInBackground:@selector(download3)
withObject:nil];
}
- (void)download1{
//图片下载原理
self.imageV = [UIImageView
new];
self.imageV.frame =
CGRectMake(0,
100, 100,
100);
[self.view
addSubview:self.imageV];
// self.imageV.image =
[UIImage imageName:这个是项目里的图片]
//下载网络图片用NSData
二进制数据0101...那种数据
//一个URL代表一个路径
不管是本地路径,远程路径都通过URL
//图片的网络路径
NSURL *url = [NSURL
URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
//计算时间方法一
NSDate *begin = [NSDate
date];
//根据图片的网络路径去下载图片数据
NSData *data = [NSData
dataWithContentsOfURL:url];
//显示图片
NSDate *end = [NSDate
date];
//计算时间间隔
NSLog(@"%f",[end
timeIntervalSinceDate:begin]);
self.imageV.image = [UIImage
imageWithData:data];
}
//计算时间方法二
- (void)download2{
self.imageV = [UIImageView
new];
self.imageV.frame =
CGRectMake(0,
100, 100,
100);
[self.view
addSubview:self.imageV];
//self.imageV.backgroundColor = [UIColor redColor];
//self.imageV.image
=
[UIImage imageName:这个是项目里的图片]
//下载网络图片用NSData
二进制数据0101...那种数据
//一个URL代表一个路径
不管是本地路径,远程路径都通过URL
//图片的网络路径
NSURL *url = [NSURL
URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
//计算时间间隔
CFTimeInterval begin =
CFAbsoluteTimeGetCurrent();
//根据图片的网络路径去下载图片数据
NSData *data = [NSData
dataWithContentsOfURL:url];
//显示图片
//计算时间间隔
CFTimeInterval end =
CFAbsoluteTimeGetCurrent();
//计算时间间隔
NSLog(@"%f",end -begin);
self.imageV.image = [UIImage
imageWithData:data];
}
- (void)download3{
//图片的网络路径
NSURL *url = [NSURL
URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
//加载图片
NSData *data = [NSData
dataWithContentsOfURL:url];
// 生成图片
UIImage *image = [UIImage
imageWithData:data];
//下载完毕要回到主线程,显示图片
//[self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
//回到主线程也可以
[self.imageV
performSelectorOnMainThread:@selector(setImage:)
withObject:nil
waitUntilDone:YES];
//waitUntilDone:YES];等这个走完,才继续走 no就是不等,一起走
// 回到主线程方法
// self.imageV performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#> modes:<#(nullable NSArray<NSString *> *)#>
// 回到主线程方法
// [self.imageV performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];
}
- (void)showImage:(UIImage *)image{
self.imageV.image = image;
// 打印线程,也可以这么打印
NSLog(@"%d",[NSThread
isMainThread]);
//线程间通信还有
不怎么用面试可以搭一答
// NSPort
// NSMessagePort
// NSMachPort
}
相关文章推荐
- 【代码笔记】iOS-离线地图
- iOS开发中本地数据存储的 ios登录成功后将用户的数据存储到本地 下次打开应用时不用重复登录了
- iOS学习笔记36-Masonry自动布局
- iOS学习笔记36-Masonry自动布局
- iOS自动打包[转载]
- iOS之NSOperation的简单介绍
- iOS 图片背景模糊效果
- HARK学习(六)--AudioStreamFromWave
- Github 上的 iOS 开源项目
- iOS关闭键盘 return 或者 点击背景
- IOS自定义控件某些区域无法点击的原因
- 关于iOS开发中图片着色(变色)的问题
- iOS 如何把view转成image,截屏分享/ 整个视图的分享。
- iOS开发- 拨打电话总结
- 配置openfire相关设置(IOS)
- iOS _BSMachError: (os/kern) invalid capability (20)
- ios 中在容器中移除单个控件的两个方法Subview
- iOS应用本地化国际化
- iOS CAAnimation 详解
- iOS上架总结