#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1.获得全局并发队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//2.使用异步函数把任务添加到队列
dispatch_async(queue, ^{
NSLog(@"下载图片---%@",[NSThread currentThread]);
//3.1 确定url
NSURL *url = [NSURL URLWithString:@"http://att2.citysbs.com/hangzhou/image/day_080715/20080715_5534d8f4da10ff9b0139wy37BTJgjFlS.jpg"];
//3.2 把二进制数据下载到本地
NSData *data = [NSData dataWithContentsOfURL:url];
//3.3 转换为图片
UIImage *image = [UIImage imageWithData:data];
//4.回到主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"刷新UI---%@",[NSThread currentThread]);
self.imageView.image = image;
});
});
}
@end