【问题标题】:UIImageView load from file path stored in Core DataUIImageView 从存储在 Core Data 中的文件路径加载
【发布时间】:2012-03-04 23:55:16
【问题描述】:

我创建了自己的自定义表格视图单元格,其中包含一个数字、一个图像(缩略图)和一个使用 Core Data 成功存储在 sqlite 数据库中的内容的描述。

下图可以更好地了解我目前在 Interface Builder 中拥有的内容:

在之前的屏幕中,我正在存储用相机拍摄的图像的文件路径并将其保存到数据库中。我希望能够在表格视图单元格上加载每个 UIImageView,并针对每个项目存储文件路径。

我尝试了以下不起作用:

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

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

【问题讨论】:

  • 你能确保thingPhotoViewpaththing都不是nil。那你能告诉我们path的值是多少吗?
  • 都不是nil,path的值为assets-library://asset/asset.JPG?id=B234919E-4D6F-4042-9731-D86EC0869550&ext=JPG

标签: iphone objective-c ios uitableview uiimageview


【解决方案1】:

我没有使用过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]);

     }];

【讨论】:

  • 很棒,就像一个魅力。我必须将 NSURL 转换为 NSString 来存储它,但只是在显示图像时再次将其转换回来,使用 NSURL *url = [[NSURL alloc] initWithString:thing.photo];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 2011-07-24
  • 2011-06-03
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多