2012-11-22 66 views
0

XCode 4.5.2;我正在從這樣的遠程服務器下載圖像:performSelectorOnMainThread調用太早

- (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。在展示圖片之前確保圖片加載正確的最佳方法是什麼?

回答

0

創建一個代理協議,在圖像下載完成時將調用回原始對象。這NSOperation tutorial有更多細節如何做到這一點

或者,使用NSOperationcompletionBlock執行圖像顯示。

+0

得到它與代表協議工作,謝謝! –