- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self performSelectorInBackground:@selector(download) withObject:nil];
}
// 下载图片
- (void)download {
NSLog(@"download - %@",[NSThread currentThread]);
// 子线程中不要改变UI界面
// 图片地址
NSString *urlString = @"http://pic1.nipic.com/2008-09-08/200898163242920_2.jpg";
// 根据地址下载图片的二进制数据
// 这句代码比较耗时
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString ]];
// 设置图片
UIImage *image = [UIImage imageWithData:data];
// 回到主线程 再刷新 UI界面 ,为了线程安全
// [self performSelectorOnMainThread:@selector(downloadFinished:) withObject:image waitUntilDone:YES];
// [self performSelector:@selector(downloadFinished:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES ];
// [self.ImageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
/**
* - (void)setImage:(UIImage *)image {
_image = image;
}
*
*/
}
- (void)downloadFinished:(UIImage *)image {
NSLog(@"downloadFinished - %@",[NSThread currentThread]);
self.ImageView.image = image;
}
线程之间的通信1
最新推荐文章于 2023-02-03 14:49:57 发布