2012-03-04 60 views
0

我已經創建了我自己的自定義表格視圖單元格,其中包含一個數字,一個圖像(縮略圖)和一些使用核心數據成功存儲在sqlite數據庫中的描述。UIImageView從核心數據中存儲的文件路徑加載

下面的圖片將給予什麼,我現在有在Interface Builder一個更好的主意: enter image description here

在屏幕前,我保存用相機拍攝的圖像的文件路徑,並保存到數據庫。我希望能夠將Table View Cells中的每個UIImageView加載到針對每個項目存儲的文件路徑中。

我試圖不工作的情況如下:

UIImageView * thingPhotoView = (UIImageView *) 
    [cell viewWithTag:11]; 

    NSString *path = thing.photo; 
    thingPhotoView.image = [UIImage imageWithContentsOfFile:path]; 
+0

可以保證'thingPhotoView','path'和'thing'都不是'nil'。那麼你能告訴我們'路徑'的價值嗎? – 2012-03-05 00:04:07

+0

他們都不是零,路徑的價值是assets-library://asset/asset.JPG?id = B234919E-4D6F-4042-9731-D86EC0869550&ext = JPG – jcrowson 2012-03-05 00:11:36

回答

2

我沒有用ALAssetLibrary但它正是你所用的是什麼URL判斷。因此,你需要使用ALAssetsLibrary訪問該文件

檢查這裏的文檔ALAssetsLibrary

您可能希望這種方法

assetForURL:resultBlock:failureBlock: 

看起來這像這樣

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; 

[library assetForURL:thing.photo 
    resultBlock:^(ALAsset *asset) { 

     thingPhotoView.image = [UIImage imageWithCGImage:[asset thumbnail]]; 

    } failureBlock:^(NSError *error) { 

     NSLog(@"Couldn't load asset %@ => %@", error, [error localizedDescription]); 

    }]; 
+0

輝煌,像魅力一樣工作。我不得不將NSURL轉換成NSString來存儲它,但是當圖像顯示時只是將它轉換回來,使用NSURL * url = [[NSURL alloc] initWithString:thing.photo]; – jcrowson 2012-03-05 00:33:42