performSelectorOnMainThread过早调用

时间:2012-11-22 20:20:15

标签: ios xcode uiimage performselector

XCode 4.5.2;我正在从这样的远程服务器下载图像:

- (void)viewDidLoad
{
[super viewDidLoad];

NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                    initWithTarget:self
                                    selector:@selector(loadImage)
                                    object:nil];

[queue addOperation:operation];
}

- (void)loadImage{
self.theobject = [RemoteQuery loadObjectWithImage:self.imageKey];

[self performSelectorOnMainThread:@selector(displayImage) withObject:nil waitUntilDone:YES];
}

-(void)displayImage{
UIImage *image = [UIImage imageWithData: self.theobject.imageData];
[self.imageView setImage:image];

}

这在IOS模拟器上运行良好,但在设备上不起作用;似乎在从[RemoteQuery loadImage]加载数据之前调用了displayImage。在显示图像之前,确保图像正确加载的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

创建一个委托协议,在图像下载完成后将回调原始对象。这个NSOperation tutorial详细介绍了如何执行此操作

或者,使用NSOperation的{​​{1}}来执行图像显示。