Search code examples
iosobjective-ccore-datauiimagerelationships

Unable to create UIImage from filepath


I'm trying to create UIImages from a list of Core Data saved file locations. Here is my code:

ontracFullScreenImageViewController *sectional = [viewControllerDictionary objectForKey:@"Sectional Appendices"];
        sectional.dataObject = self.dataObject.dataPack;
        //sectional.imageArray = [[dataObject.sectionalDiagrams allObjects] mutableCopy];



        if ([dataObject.sectionalDiagrams count] != 0 || dataObject.sectionalDiagrams == nil){
        NSLog(@"dataObject.sectionalDiagrams %@", dataObject.sectionalDiagrams);
            for (SectionalDiagram *section in dataObject.sectionalDiagrams) {
                NSLog(@"section %@", section);
                UIImage *image = [UIImage imageWithContentsOfFile:section.locationString];

                NSLog(@"image %@", image);

                if(image != nil){
                    [sectional.imageArray addObject:image];
                    [sectional.imageLinks addObject:section.locationString];
                }



            }
            //[sectional loadScreen];

        }
        else
        {
            [self removeMenuOptionForString:@"Sectional Appendices"];
        }
        [TestFlight passCheckpoint:@"Loaded the Sectional Appendices"];

The log shows the following objects with the Core Data request:

with objects {(
<SectionalDiagram: 0x14fdde40> (entity: SectionalDiagram; id: 0x14fddb10 <x-coredata://BC0EFCDC-FAA8-404D-A9DE-33AEE09AD1C6/SectionalDiagram/p15> ; data: {
dataObject = "0x14d29820 <x-coredata://BC0EFCDC-FAA8-404D-A9DE-33AEE09AD1C6/DataObject/p17>";
locationString = "/var/mobile/Containers/Data/Application/43B4BC10-58AA-45DE-9A3B-E90A3A08DC2D/Documents/[email protected]_A2rA2d_sectional_appendix_947735_2147483647_image.png";
}),
<SectionalDiagram: 0x14fddbd0> (entity: SectionalDiagram; id: 0x14fddaf0 <x-coredata://BC0EFCDC-FAA8-404D-A9DE-33AEE09AD1C6/SectionalDiagram/p13> ; data: {
dataObject = "0x14d29820 <x-coredata://BC0EFCDC-FAA8-404D-A9DE-33AEE09AD1C6/DataObject/p17>";
locationString = "/var/mobile/Containers/Data/Application/43B4BC10-58AA-45DE-9A3B-E90A3A08DC2D/Documents/[email protected]_NpH3Yg_sectional_appendix_947735_2147483647_image.png";
}),
<SectionalDiagram: 0x14fdde10> (entity: SectionalDiagram; id: 0x14fddb00 <x-coredata://BC0EFCDC-FAA8-404D-A9DE-33AEE09AD1C6/SectionalDiagram/p14> ; data: {
dataObject = "0x14d29820 <x-coredata://BC0EFCDC-FAA8-404D-A9DE-33AEE09AD1C6/DataObject/p17>";
locationString = "/var/mobile/Containers/Data/Application/43B4BC10-58AA-45DE-9A3B-E90A3A08DC2D/Documents/[email protected]_jGKgWx_sectional_appendix_947735_2147483647_image.png";
})

I have checked and all three of the images exist on the device but it keeps coming back as null.


Solution

  • The problem is that the path to the Documents directory can change, particularly when rebuilding the app. So don't store the full path to the file. Just store the filename. To get the image, lookup the path for the documents directory, using

    [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]
    

    and append the filename.