线程之间的通讯

- (IBAction)action:(id)sender {
    NSLog(@"begin%@",[NSDate date]);
    NSDate * begin =[NSDate date];
    NSData * dat =[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img.tuku.cn/file_thumb/201501/m2015013000131833.jpg"]];
    self.imageview.image=[UIImage imageWithData:dat];
    NSLog(@"endn%@",[NSDate date]);
    NSDate * end =[NSDate date];
    NSLog(@"%f",[end timeIntervalSinceDate:begin]);
    CFTimeInterval  start =CFAbsoluteTimeGetCurrent();
    
    
    [self performSelectorInBackground:@selector(down) withObject:nil];
    
    
}

-(void)down
{
    NSData * dat =[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://b.hiphotos.baidu.com/zhidao/pic/item/1e30e924b899a9010d59b4f31f950a7b0308f583.jpg"]];
    NSLog(@"%@",[NSThread currentThread]);
    UIImage* image=[UIImage imageWithData:dat];
    //回到主线程
//    [self.imageview performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
    [self.imageview performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO ];
}

GCD通讯

   dispatch_queue_t queue=  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
   dispatch_async(queue, ^{
       NSData * dat =[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://b.hiphotos.baidu.com/zhidao/pic/item/1e30e924b899a9010d59b4f31f950a7b0308f583.jpg"]];
       NSLog(@"当前线程===%@",[NSThread currentThread]);
       UIImage* image=[UIImage imageWithData:dat];
       
//       self.imageview.image=image ;

       dispatch_async(dispatch_get_main_queue(), ^{
           self.imageview.image=image ;
           NSLog(@"返回===%@",[NSThread currentThread]);

       });
       
       NSLog(@"========");
   });

 

posted @ 2016-04-24 20:50  谢小锋  阅读(141)  评论(0)    收藏  举报