我使用 NSOperationQueue 加载 UIImageView。
加载从 Internet 获取图像,然后将其添加到图像视图。我遇到的问题是该方法完成但图像视图需要大约 3 秒后才能实际显示图像或从超级视图中删除图像视图......
- (void)viewDidLoad { NSLog(@"AirportDetailView: viewDidLoad");
[super viewDidLoad];
[self.activity startAnimating];
self.queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImage) object:NULL];
[self.queue addOperation:operation];
[operation release];
}
-(void)loadImage {
[myAp ImageForAp];
NSLog(@"ImageFor Ap Ended, %@",myAp.ApDiagram);
[self.activity stopAnimating];
if (myAp.ApDiagram==NULL) {
NSLog(@"Finally Gets Here");
[self.Diagram removeFromSuperview];
}
else {
NSLog(@"Finally Gets Here with Diag");
[self.Diagram setBackgroundImage:myAp.ApDiagram forState:UIControlStateNormal];
}
}
NSLOG 显示前两个日志语句之间的延迟大约 3 秒无法理解为什么....
用我的最新代码更新......
-(void)loadImage {
[myAp ImageForAp];
NSLog(@"ImageFor Ap Ended, %@",myAp.ApDiagram);
[self performSelectorOnMainThread:@selector(UpdateUI) withObject:nil waitUntilDone:NO];
}
-(void)UpdateUI {
[self.activity stopAnimating];
if (myAp.ApDiagram==NULL) {
NSLog(@"Finally Gets Here");
[self.Diagram removeFromSuperview];
}
else {
NSLog(@"Finally Gets Here with Diag");
[self.Diagram setBackgroundImage:myAp.ApDiagram forState:UIControlStateNormal];
}
}