UIImageView从核心数据中存储的文件路径加载

问题描述:

我已经创建了我自己的自定义表格视图单元格,其中包含一个数字,一个图像(缩略图)和一些使用核心数据成功存储在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

我没有用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