2014-09-30 27 views
0

我有一個非常簡單的iOS應用程序(從基本的主細節樣板改編)。UIImage從託管對象數據加載不顯示在UIImageView

我已經將RestKit設置爲從服務器加載數據。如果對象的圖像URL得到更新,我下載圖像(使用AFHTTPClient子類)並使用UIImagePNGRepresentation(image)保存其數據。非常簡單。

所以現在,我已經有一個已經填充了對象的數據庫 - 包括他們的imageData。但由於某種原因,雖然我可以從數據中獲得UIImage實例,但UIImage不會顯示在UIImageView中。

我已經有了自動生成NSManagedObject的子類,其中(除其他事項外)拉的圖像數據的類別,並返回一個UIImage實例:

@implementation Artwork (Helpers) 
// ... 

- (UIImage*)image { 
    if (self.imageData) { 
     return [UIImage imageWithData:self.imageData]; 
    } 
    return nil; 
} 

@end 

在我的詳細視圖,我有一個UIImageView,其image由上述方法設定。這是來自我的詳細視圖控制器的相關位。它在segue之前被調用,並且可以很好地用於設置描述文本,但不會正確設置圖像。

- (void)configureView { 
    // Update the user interface for the detail item (a Artwork instance in this case). 
    if (self.detailItem) { 
     // this works just fine 
     self.detailDescriptionText.text = self.detailItem.rawDescription; 

     // ... but this doesn't! Nothing is shown in the 
     UIImage *image = self.detailItem.image; 
     if (image) { 
      // Yes, the UIImage *is* there 
      NSLog(@"UIImage instance: %@, size: %fx%f", image, image.size.width, image.size.height); 
      // ... but this doesn't seem to any effect 
      self.imageView.image = image; 
     } 
    } 
} 

NSLog調用打印:

UIImage instance: <UIImage: 0x109a0d090>, size: 533.000000x300.000000

所以它肯定好像UIImage對象存在,並從數據已被解就像它應該。但在UIImageView中沒有任何顯示。

有趣的是,如果我設置了一個簡單的觸摸聽者詳細視圖控制器上,我可以示出了使用完全相同的代碼的圖像:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UIImage *image = self.detailItem.image; 
    if (image) { 
     NSLog(@"UIImage instance: %@, size: %fx%f", image, image.size.width, image.size.height); 
     self.imageView.image = image; 
    } 
} 

即完美地工作 - 敲擊屏幕和圖像立即顯示了起來,和NSLog調用打印:

UIImage instance: <UIImage: 0x10980a7e0>, size: 533.000000x300.000000

所以真的圖像數據,並將其確實得到解壓到適當的UIImage - 但它不會顯示。

因此,總而言之,似乎存在某種計時或線程問題。但在這裏我畫了一個空白。

+0

嘗試設置在主線程中圖片上的圖像:dispatch_async(dispatch_get_main_queue()^(無效){/ *你的代碼在這裏* /} ); – 2014-09-30 16:28:47

+0

@AaronWojnowski請讓這個答案,我可以接受它:)我實際上嘗試過(發現類似的問題和答案),但無論出於何種原因,應用程序只是掛了,當我做。但現在它工作正常,確實設置正確的圖像 – Flambino 2014-09-30 16:32:39

+0

美麗,很高興它的作品 - 完成! – 2014-09-30 16:33:35

回答

0

確保設置在主線程:)

dispatch_async(dispatch_get_main_queue(), ^(void) { 
    /* your code here */ 
}); 
相關問題