线程7--GCD的基本使用

子线程执行延时操作,执行完成后返回主线程更新界面

 dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//子线程;
    dispatch_async(queue, ^{
        NSLog(@"%@---",[NSThread currentThread]);
        NSURL *url=[NSURL URLWithString:@"http://img.hb.aicdn.com/1f2acbecdc6ac6187a9b02bffe4a5dedc9fe45ff15a44-D6LjC1_fw580"];
        NSData *data=[NSData dataWithContentsOfURL:url];
        UIImage *image=[UIImage imageWithData:data];
        NSLog(@"图片加载完毕");
/*************************开启子线程,加载图片*****************/
        //[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
/***********************************************************/
/*要求使用GCD的方式,在子线程加载图片完毕后,主线程拿到加载的image刷新UI界面。
 
 线程间通信
 
 从子线程回到主线程
 */
        
        dispatch_queue_t queuemain=dispatch_get_main_queue();//主线程;
        dispatch_async(queuemain, ^{
            self.imageView.image=image;
            NSLog(@"%@",[NSThread currentThread]);
            
        });
 /**********************************************************/
    });
  
原文地址:https://www.cnblogs.com/sunjianfei/p/5725069.html