zoukankan      html  css  js  c++  java
  • 线程通信(NSThread,NSOperation)

    - (void)viewDidLoad {

        [super viewDidLoad];

        [NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];

        

        

       }

    -(void)downloadImage

    {

        NSURL *url = [NSURL URLWithString:IMAGE_URL];

        NSData *data = [NSData dataWithContentsOfURL:url];

        UIImage *image = [UIImage imageWithData:data];

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

        [self performSelector:@selector(loadImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];

    //    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];

        NSLog(@"1111");

        

    }

    -(void)loadImage:(id)obj

    {

        self.imageView.image = obj;

    }

    //NSOperation线程间通信

     NSOperationQueue *queue = [[NSOperationQueue alloc]init];

        [queue addOperationWithBlock:^{

            NSURL *url = [NSURL URLWithString:IMAGE_URL];

            NSData *data = [NSData dataWithContentsOfURL:url];

            UIImage *image = [UIImage imageWithData:data];

            NSLog(@"--%@--",[NSThread currentThread]);

            [[NSOperationQueue mainQueue]addOperationWithBlock:^{

                self.imageView.image = image;

            }];

        }];

  • 相关阅读:
    post和get区别
    https
    tcp/ip协议
    webpack与gulp的不同
    什么是webpack
    spring boot 输入参数统一校验
    spring boot++jpa+ mysql +maven
    Intellij IDEA 2018.2.2 SpringBoot热启动 (Maven)
    git 从远程仓克隆到本地新分支
    ASP.NET MVC 自动模型验证
  • 原文地址:https://www.cnblogs.com/PJXWang/p/5949283.html
Copyright © 2011-2022 走看看