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;

            }];

        }];

  • 相关阅读:
    冲刺——第三天
    冲刺——第二天
    梦断代码前三章略有感想
    四则运算法则设计思路
    第一期阅读计划
    软件工程概论第一次课堂小测-------产生30个100以内的随机整数四则运算的小程序
    软件演化
    软件测试
    软件实现
    面向对象设计
  • 原文地址:https://www.cnblogs.com/PJXWang/p/5949283.html
Copyright © 2011-2022 走看看