import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIImageView * imageView;
@property(nonatomic,strong)NSData * imageData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imageView =[[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.view addSubview:self.imageView];
}
pragma mark ----创建子线程加载数据
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self performSelectorInBackground:@selector(loadimage) withObject:nil];
}
-(void)loadimage{
NSLog(@"1: %@ \n %d",[NSThread currentThread],[NSThread isMainThread]);
NSString * path = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497350942852&di=f2ac74a4d80ee6a0a4024e9e68204495&imgtype=0&src=http%3A%2F%2Fimg5q.duitang.com%2Fuploads%2Fitem%2F201505%2F08%2F20150508173414_uxvTU.jpeg";
NSURL * url = [NSURL URLWithString:path];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
self.imageData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//进入主线程 刷新UI
[self performSelectorOnMainThread:@selector(reloadimage) withObject:nil waitUntilDone:YES];
}
-(void)reloadimage{
NSLog(@"2: %@ \n%d",[NSThread currentThread],[NSThread isMainThread]);
//imageView 数据显示
self.imageView.image = [UIImage imageWithData:self.imageData];
}