简体   繁体   English

UIImageView从存储在Core Data中的文件路径加载

[英]UIImageView load from file path stored in Core Data

I've created my own custom Table View Cell comprising of a number, an image (thumbnail) and a description of something that is successfully being stored in a sqlite database using Core Data. 我创建了自己的自定义表格视图单元格,包括数字,图像(缩略图)以及使用Core Data成功存储在sqlite数据库中的内容的描述。

The image below will give a better idea of what I currently have in Interface Builder: 下面的图片将更好地了解我目前在Interface Builder中拥有的内容: 在此输入图像描述

In the screen before, I am storing the filepath of an image taken with the camera and saving it to the database. 在之前的屏幕中,我存储了用相机拍摄的图像的文件路径并将其保存到数据库中。 I want to be able to load each UIImageView on the Table View Cells with the filepaths stored against each item. 我希望能够在Table View Cells上加载每个UIImageView,并为每个项目存储文件路径。

I have tried the following which does not work: 我尝试了以下不起作用:

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

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

I've not used ALAssetLibrary but judging by the URL that is what you are using. 我没有使用ALAssetLibrary而是根据您正在使用的URL来判断。 Therefore you will need to use ALAssetsLibrary to access the file 因此,您需要使用ALAssetsLibrary来访问该文件

Check the docs here ALAssetsLibrary 检查ALAssetsLibrary中的文档

You probably want this method 你可能想要这种方法

assetForURL:resultBlock:failureBlock:

Which might look like this 这可能是这样的

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]);

     }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM