简体   繁体   中英

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.

The image below will give a better idea of what I currently have in 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.

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. Therefore you will need to use ALAssetsLibrary to access the file

Check the docs here 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]);

     }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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