NSString * str = @"http://img.tupianzj.com/uploads/allimg/160601/9-160601091U8.jpg";
[NSThread detachNewThreadSelector:@selector(downLoadImage:) toTarget:self withObject:str];
//在子线程下载图片,发送同步连接请求数据//子线程中系统不会帮我们生成自动释放池,需要我们手动添加
- (void)downLoadImage:(NSString *)imageURLStr
{
@autoreleasepool {
NSURL * url = [NSURL URLWithString:imageURLStr];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
UIImage * image = [UIImage imageWithData:data];
//注意:对于界面的刷新,交给主线程来处理,通过调用方法,回到主线程
//performSelectorOnMainThread: 在主线程完成相应的任务,
[self performSelectorOnMainThread:@selector(refreshUI:) withObject:image waitUntilDone:NO]; //waitUntilDone: YES等待这个方法执行完再执行(refreshUI:) NO:不等
}
}
//主线程刷新UI
- (void)refreshUI:(UIImage *)Aimage{
self.image.image = Aimage;
}
网友评论