1惋戏、獲得全局隊(duì)列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//使用異步函數(shù)+并發(fā)隊(duì)列開線程現(xiàn)在圖片
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;
});
});
總結(jié):使用異步函數(shù)+全局隊(duì)列下載圖片不會占用主線程的時間笆载,當(dāng)圖片下載完成的時候回到主線程刷新UI,展示圖片