NSThread线程间通信

作者: BEYOND黄 | 来源:发表于2017-05-29 19:18 被阅读57次

线程间通信:1.一个线程传递数据给另一个线程 2.一个线程执行完任务后转到下一个线程继续执行任务。

常用方法:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullableid)arg waitUntilDone:(BOOL)wait modes:(nullableNSArray *)array;

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullableid)arg waitUntilDone:(BOOL)wait;// equivalent to the first method with kCFRunLoopCommonModes

小例子:图片下载

#import"ViewController.h"

@interfaceViewController()

@property(weak,nonatomic)IBOutletUIImageView*imageView;

@end

@implementationViewController

#pragma mark ----------------------

#pragma Events

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event

{

[NSThreaddetachNewThreadSelector:@selector(download)toTarget:selfwithObject:nil];

}

#pragma mark ----------------------

#pragma Methods

//开子线程下载图片,回到主线程刷新UI

-(void)download

{

//1.确定URL

NSURL*url = [NSURLURLWithString:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.700_0.jpeg"];

//2.根据url下载图片二进制数据到本地

NSData*imageData = [NSDatadataWithContentsOfURL:url];

//3.转换图片格式

UIImage*image = [UIImageimageWithData:imageData];

NSLog(@"download----%@",[NSThreadcurrentThread]);

//4.回到主线程显示UI

/*

第一个参数:回到主线程要调用哪个方法

第二个参数:前面方法需要传递的参数此处就是image

第三个参数:是否等待

*/

//[self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:NO];

//[self performSelector:@selector(showImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];

[self.imageViewperformSelectorOnMainThread:@selector(setImage:)withObject:imagewaitUntilDone:YES];

//self.imageView.image = image;

NSLog(@"---end---");

}

//计算代码段执行时间的第一种方法

-(void)download1

{

//0.000018

//0.166099

//1.确定URL

NSURL*url = [NSURLURLWithString:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.700_0.jpeg"];

NSDate*start = [NSDatedate];//获得当前的时间

//2.根据url下载图片二进制数据到本地

NSData*imageData = [NSDatadataWithContentsOfURL:url];

NSDate*end = [NSDatedate];//获得当前的时间

NSLog(@"%f",[endtimeIntervalSinceDate:start]);

//3.转换图片格式

UIImage*image = [UIImageimageWithData:imageData];

//4.显示UI

self.imageView.image= image;

}

//计算代码段执行时间的第二种方法

-(void)download2

{

//1.确定URL

NSURL*url = [NSURLURLWithString:@"http://img4.duitang.com/uploads/blog/201310/18/20131018213446_smUw4.thumb.700_0.jpeg"];

CFTimeIntervalstart =CFAbsoluteTimeGetCurrent();

//2.根据url下载图片二进制数据到本地

NSData*imageData = [NSDatadataWithContentsOfURL:url];

CFTimeIntervalend =CFAbsoluteTimeGetCurrent();

NSLog(@"end-start = %f---%f---%f",end - start,end,start);

//3.转换图片格式

UIImage*image = [UIImageimageWithData:imageData];

//4.显示UI

self.imageView.image= image;

}

//更新UI操作

-(void)showImage:(UIImage*)image

{

self.imageView.image= image;

NSLog(@"UI----%@",[NSThreadcurrentThread]);

}

相关文章

  • 《iOS高级开发之多线程编程之二》

    线程间的通信 在一个进程中,线程往往不是孤立存在的,多个线程之间经常进行通信,称为线程间通信。 NSThread ...

  • NSThread线程间通信

    线程间通信:1.一个线程传递数据给另一个线程 2.一个线程执行完任务后转到下一个线程继续执行任务。 常用方法: -...

  • 线程间的通信

    1>NSThread的线程间的通信将一个线程执行的结果, 传到另外一个线程 (不同的线程之间的通信都是线程通信)S...

  • iOS线程之间的通信

    线程间通讯 一、NSThread 1.简单说明 ①线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需...

  • iOS线程间通信 - NSThread篇

    线程间通信常用方法: - (void)performSelectorOnMainThread:(SEL)aSele...

  • iOS开发-多线程知识点

    一. NSThread 面向对象的,需要程序员手动创建线程,但不需要手动销毁。子线程间通信很难. 1.创建子线程,...

  • iOS - 线程 / 进程 的通信

    1. 线程中的通信 线程中通信的体现 在iOS多线程开发中,有NSThread、GCD、NSOpeartion几种...

  • IOS多线程-线程间的通信

    使用NSThread实现线程间的通信 实现效果 ⚠️注意!!! 如果点击屏幕发现不能正常下载图片,并且报以下错误的...

  • NSThread三种创建线程的方法

    对耗时操作的理解 NSThread基础 线程控制 线程安全 线程间同行

  • 多线程(Pthread/NSThread)

    Pthread简单使用 NSThread基本使用 线程的状态 线程的安全 线程间通讯

网友评论

    本文标题:NSThread线程间通信

    本文链接:https://www.haomeiwen.com/subject/zzclfxtx.html