线程间通信常用方法:
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;
开子线程下载数据,在主线程刷新UI;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//NSOperation 篇
// [self downLoadImageAndShow];
//NSThread 篇
[self performSelectorInBackground:@selector(downLoad) withObject:nil];
}
#pragma mark - //NSThread 篇
- (void)downLoad{
NSURL * url = [NSURL URLWithString:@"https://zuoye2.xinkaoyun.com/awm/35/iOS_09060620200720110101.jpeg"];
NSData * data = [NSData dataWithContentsOfURL:url];
UIImage * image = [UIImage imageWithData:data];
[self performSelectorOnMainThread:@selector(iconSetImage:) withObject:image waitUntilDone:NO];
}
- (void)iconSetImage:(UIImage *)image{
self.iconImageView.image = image;
}

网友评论