- 获取一个全局的队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
- 先开启一个线程,把下载图片的操作放在子线程中处理
dispatch_async(queue, ^{
1.下载图片
NSURL *url = [NSURL URLWithString:@"https://imgsa.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=5d82ca9a75d98d1062d904634056d36b/34fae6cd7b899e517cec66724ba7d933c8950d71.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
NSLog(@"下载操作所在的线程--%@",[NSThread currentThread]);
2.回到主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
NSLog(@"刷新UI---%@",[NSThread currentThread]);
});
});