#import "ViewController.h"
#define IMAGE_URL @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1505384144967&di=c99009f5c44b8bca603bd3aef02726d5&imgtype=0&src=http%3A%2F%2Ftupian.enterdesk.com%2Fuploadfile%2F2012%2F1011%2F20121011092237493.jpg"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[NSThread detachNewThreadSelector:@selector(downLoadImage) toTarget:self withObject:nil];
}
-(void)downLoadImage {
NSURL *url = [NSURL URLWithString:IMAGE_URL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
//一、线程通信 由子线程回归到主线程
// [self performSelectorOnMainThread:@selector(loadImage:) withObject:image waitUntilDone:NO];
//二、第二种方式
// [self performSelector:@selector(loadImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];
//三、第三种方式
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
NSLog(@"waitUntilDone:NO时才会先执行");
}
-(void)loadImage:(id)obj {
self.imageView.image = obj;
}
@end
运行效果: