iOS:OC--NSThread

  • 原理分析

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //打印当前线程
    NSLog(@"current = %@",[NSThread currentThread]);
    //获取主线程
    NSLog(@"%@",[NSThread mainThread]);
    //判断当前是否为主线程
    NSLog(@"%d",[NSThread isMainThread]);
    
    #pragma mark --- 手动开启子线程
    //1.创建线程对象
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(sayHi) object:nil];
    //.线程休眠
//    [NSThread sleepForTimeInterval:5];
    //开启线程
    [thread start];
    //结束当前线程
//    [thread cancel];
    //退出当前线程
//    [NSThread exit];
#pragma mark --- 自动开启子线程;
    [NSThread detachNewThreadSelector:@selector(sayHi) toTarget:self withObject:nil];
    
}
-(void)sayHi
{
    @autoreleasepool {
        NSLog(@"current =  %@ %@ %d",[NSThread currentThread],[NSThread mainThread],[NSThread isMainThread]);
    }
    //回到主线程修改当前背景颜色
    //回到主线程方法
    //是否等待子线程完成之后进入主线程
    [self performSelectorOnMainThread:@selector(changeColor) withObject:nil waitUntilDone:YES];
}
-(void)changeColor
{
    self.view.backgroundColor = [UIColor cyanColor];
    NSLog(@"current = %@ main = %@",[NSThread currentThread],[NSThread mainThread]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
  • NSThread实现添加图片
#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)UIImageView* imageView;
@property(nonatomic,strong)NSData* imageData;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageView = [[UIImageView alloc]initWithFrame:([UIScreen mainScreen].bounds)];
    [self.view addSubview:self.imageView];
}
#pragma mark --- 创建子线程加载数据
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self performSelectorInBackground:@selector(loadImage) withObject:nil];
}
-(void)loadImage
{
    NSString *path = @"http://img4.imgtn.bdimg.com/it/u=2730485761,138060261&fm=26&gp=0.jpg";
    NSURL *url = [NSURL URLWithString:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    self.imageData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //进入主线程,刷新 UI
    [self performSelectorOnMainThread:@selector(reloadImage) withObject:nil waitUntilDone:YES];
}
-(void)reloadImage
{
    NSLog(@"%@ \n %d",[NSThread currentThread],[NSThread isMainThread]);
    //imageView数据源展示
    self.imageView.image = [UIImage imageWithData:self.imageData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 24,089评论 8 183
  • 1、简介:1.1 iOS有三种多线程编程的技术,分别是:1.、NSThread2、Cocoa NSOperatio...
    LuckTime阅读 1,367评论 0 1
  • 1、截取字符串”20 | http://www.baidu.com”中,”|”字符前面和后面的数据,分别输出它们 ...
    强子ly阅读 3,013评论 8 46
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,209评论 30 471
  • 给孩梳头的时候,小家伙不闲着,扭动不停,生命不止,一脚深踩在酸奶瓶上,散了满地… 结果我没能控制自己的情绪:@*#...
    放松身心阅读 139评论 0 0