ios多线程GCD下载图片

 1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 2 {
 3     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 4     dispatch_async(queue, ^{
 5         NSLog(@"--download--%@", [NSThread currentThread]);
 6         // 下载图片
 7         NSURL *url = [NSURL URLWithString:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
 8         NSData *data = [NSData dataWithContentsOfURL:url]; // 这行会比较耗时
 9         UIImage *image = [UIImage imageWithData:data];
10         
11         // 回到主线程显示图片
12         dispatch_async(dispatch_get_main_queue(), ^{
13             NSLog(@"--imageView--%@", [NSThread currentThread]);
14             self.imageView.image = image;
15         });
16     });
17 }

 

posted @ 2016-01-25 15:38  xiaocaoera  阅读(295)  评论(0编辑  收藏  举报