IOS 线程间通信(练习)

//
//  ViewController.m
//  NSopertion
//
//  Created by apple on 15/5/4.
//  Copyright (c) 2015年 apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIImageView *uiv;
@end
@implementation ViewController
-(UIImageView *)uiv
{
    if (_uiv == nil)
    {
        _uiv = [[UIImageView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:_uiv];
    }
    return  _uiv;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    NSOperationQueue *ququ =[[NSOperationQueue alloc] init];
//    
//    NSInvocationOperation *ni = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil];
//    
////    [ni start];
//    [ququ addOperation:ni];
//    NSOperationQueue *qu1 = [[NSOperationQueue alloc] init];
//    
//    
//    NSBlockOperation *nsblock = [NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"%@",[NSThread currentThread]);
//    }];
////    [nsblock addExecutionBlock:^{
////        NSLog(@"%@",[NSThread currentThread]);
////    }];
////    [nsblock start];
//    [qu1 addOperation:nsblock];
    
//    http://d.hiphotos.baidu.com/image/pic/item/342ac65c1038534398af4f3c9113b07eca80884f.jpg
    
    
    
//    UIView *v = [[UIView alloc] initWithFrame:self.view.bounds];
  
//    
//        NSBlockOperation *nbs = [NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"download-----%@",[NSThread currentThread]);
//        NSURL *u = [NSURL URLWithString:@"http://d.hiphotos.baidu.com/image/pic/item/342ac65c1038534398af4f3c9113b07eca80884f.jpg"];
//        NSData *d = [NSData dataWithContentsOfURL:u];
//        
//        UIImage *ui = [[ UIImage alloc] initWithData:d ];
//            [self performSelector:@selector(updateui:) onThread:[NSThread mainThread] withObject:ui waitUntilDone:nil];
//        
//    }];
//   
//    NSOperationQueue *qu1 = [[NSOperationQueue alloc] init];
//    [qu1 addOperation:nbs];
//    
//    
//    
//    UIImageView *uic = [[UIImageView alloc] initWithFrame:self.view.bounds];
//    [self.view addSubview:uic];
//    
//    
//    dispatch_queue_t my1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    
//    dispatch_async(my1, ^{
//            NSLog(@"download-----%@",[NSThread currentThread]);
//            NSURL *u = [NSURL URLWithString:@"http://d.hiphotos.baidu.com/image/pic/item/342ac65c1038534398af4f3c9113b07eca80884f.jpg"];
//            NSData *d = [NSData dataWithContentsOfURL:u];
//            
//            UIImage *ui = [[ UIImage alloc] initWithData:d ];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                uic.image = ui;
//                NSLog(@"%@",[NSThread currentThread]);
//            
//            });
//    });
        NSURL *url = [NSURL URLWithString:@"http://d.hiphotos.baidu.com/image/pic/item/342ac65c1038534398af4f3c9113b07eca80884f.jpg"];
//
//    NSInvocationOperation *iv = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download:) object:url];
//    
//    NSOperationQueue *quer1 = [[NSOperationQueue alloc ] init];
//    [quer1 addOperation:iv];
//    
    NSOperationQueue *qu2 = [[NSOperationQueue alloc] init];
    NSInvocationOperation *ivq = [[NSInvocationOperation alloc ] initWithTarget:self selector:@selector(download:) object:url];
    
    [qu2 addOperation:ivq];
    
    
    
}
- (void)download:(NSURL *)u
{
    NSLog(@"download-----%@",[NSThread currentThread]);
//    NSURL *u = [NSURL URLWithString:@"http://d.hiphotos.baidu.com/image/pic/item/342ac65c1038534398af4f3c9113b07eca80884f.jpg"];
    NSData *d = [NSData dataWithContentsOfURL:u];
    
    UIImage *ui = [[ UIImage alloc] initWithData:d ];
//    [self performSelector:@selector(updateui) onThread:[NSThread mainThread] withObject:nil waitUntilDone:nil modes:nil];
//[self performSelector:@selector(updateui:) onThread:[NSThread mainThread] withObject:ui waitUntilDone:YES];
//    [self performSelectorOnMainThread:@selector(updateui:) withObject:ui waitUntilDone:YES];
  
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        self.uiv.image = ui;
        NSLog(@"%@",[NSThread currentThread]);
    }];
}
- (void)updateui:(UIImage *)image2
{
    
    self.uiv.image = image2;
    NSLog(@"%@",[NSThread currentThread]);
    
}
    
@end

你可能感兴趣的:(ios,oc,线程间通信)