GCD多线程 在子线程中获取网络图片 在主线程更新

子线程中得所有数据都可以直接拿到主线程中使用
//当触摸屏幕的时候,从网络上下载一张图片到控制器的view上显示
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
     //获取一个全局并行队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     //把任务添加到队列中执行
     dispatch_async(queue, ^{
         //打印当前线程
         NSLog(@"%@",[NSThread currentThread]);
       //从网络上下载图片
         NSURL *urlstr=[NSURL URLWithString:@"http://h.hiphotos.baidu.com/baike/w%3D268/sign=30b3fb747b310a55c424d9f28f444387/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
         NSData *data=[NSData dataWithContentsOfURL:urlstr];
UIImage *image=[UIImage imageWithData:data]; //提示 NSLog(@"图片加载完毕"); dispatch_async(dispatch_get_main_queue(), ^{ self.imageView.image=image; //打印当前线程 NSLog(@"%@",[NSThread currentThread]); }); }); }
 

posted on 2015-11-16 15:00  zero_chan  阅读(158)  评论(0编辑  收藏  举报

导航