1、获得全局队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//使用异步函数+并发队列开线程现在图片
dispatch_async(queue, ^{
NSLog(@"download----%@",[NSThread currentThread]);
NSURL *url = [NSURL URLWithString:@"http://www.chinanews.com/cr/2014/0108/1576296051.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
//回到主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"UI----%@",[NSThread currentThread]);
self.imageView.image = image;
});
});
总结:使用异步函数+全局队列下载图片不会占用主线程的时间,当图片下载完成的时候回到主线程刷新UI,展示图片