您的位置:首页 > 移动开发 > IOS开发

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: