#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.imageView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:@"http://article.fd.zol-img.com.cn/t_s500x2000/g4/M08/04/06/Cg-4WlPoSQaIJMNHAAFQxnn-qSoAAQZGQCqggkAAVDe827.jpg"];
NSData *imageData= [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
NSLog(@"Download-----%@",[NSThread currentThread]);
dispatch_sync(dispatch_get_main_queue(), ^{
self.imageView.image = image;
NSLog(@"UI-----%@",[NSThread currentThread]);
});
});
}
- (UIImageView *)imageView {
if (!_imageView) {
_imageView = [[UIImageView alloc] init];
_imageView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
_imageView.frame = CGRectMake(100, 100, 200, 200);
}
return _imageView;
}
@end