• 线程间的通信(3种方式)


    三种方法都是通过touchesBegin监听屏幕的触摸实现

    一、performSelector方式

     1 #import "ViewController.h"
     2 @interface ViewController ()
     3 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
     4 @end
     5 @implementation ViewController
     6 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
     7 {
     8     //放入子线程
     9     [self performSelectorInBackground:@selector(download3) withObject:nil];
    10 }
    11 
    12 //下载放入子线程,显示图片应该放在主线程!!!否则会导致刷新问题
    13 - (void)download3
    14 {
    15     //图片的网络路径
    16     NSURL *url = [NSURL URLWithString:@"http://ww2.sinaimg.cn/mw690/63e6fd01jw1f3f3rf75goj20qo0zkagy.jpg"];
    17     //下载图片数据
    18     NSData *data = [NSData dataWithContentsOfURL:url];
    19     
    20     //生成图片
    21     UIImage *image = [UIImage imageWithData:data];
    22     //回到主线程显示图片方法一:
    23 //    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
    24     //回到主线程显示图片方法二:
    25     //waitUntilDone:表示是否等待主线程做完事情后往下走,YES表示做完后执行下面事,NO表示跟下面事一起执行
    26     [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
    27     //回到主线程显示图片方法三:
    28     [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
    29 }
    30 //主线程显示图片
    31 - (void)showImage:(UIImage *)image
    32 {
    33     self.imageView.image = image;
    34 }

    二、GCD方式

     1 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
     2 {
     3     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
     4         //图片的网络途径
     5         NSURL *url = [NSURL URLWithString:@"http://ww2.sinaimg.cn/mw1024/75614297jw1f34e5llyz4j20qo0zj0zl.jpg"];
     6         //加载图片
     7         NSData *data = [NSData dataWithContentsOfURL:url];
     8         //生成图片
     9         UIImage *image = [UIImage imageWithData:data];
    10         //回到主线程
    11         dispatch_async(dispatch_get_main_queue(), ^{
    12             self.imageView.image = image;
    13         });
    14     });
    15     
    16 }

    三、operation方式(此种方式更具有面向对象特性!)

     1 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
     2 {
     3     //直接开始子线程执行任务
     4     [[[NSOperationQueue alloc] init] addOperationWithBlock:^{
     5         NSURL *url = [NSURL URLWithString:@"http://ww4.sinaimg.cn/mw690/63e6fd01jw1ezxz499hy5j21gt0z94qq.jpg"];
     6         NSData *data = [NSData dataWithContentsOfURL:url];
     7         UIImage *image = [UIImage imageWithData:data];
     8         //回到主线程
     9         [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    10             //显示图片
    11             self.imageView.image = image;
    12         }];
    13     }];
    14 }

    以上三种方式都需要在main storyboard中拖一个imageView,然后设置自动布!!

  • 相关阅读:
    TypeError: run() missing 2 required positional arguments: 'rerun' and 'save_last_run'
    在wsl的ubuntu上安装vcpkg
    vscode + WSL +Ubuntu编程解决方案
    clion debug模式带参数运行程序
    关于jdk1.7之后String的intern方法的一点理解
    关于java中split的坑
    关于向HashMap存放数据出现顺序混乱的问题
    oracle外键禁用
    oracle复杂查询(二)
    oracle复杂查询(一)
  • 原文地址:https://www.cnblogs.com/hissia/p/5484724.html
Copyright © 2020-2023  润新知