Problems starting Uiimage with image path from core data

Asked

Viewed 54 times

0

I downloaded an image and stored it on disk. After downloading the class inserts the image path into the core data, this string:

@"/Users/Tiago/Library/Developer/CoreSimulator/Devices/3E103B07-E48E-4740-911C-ECD24E0C3A3F/data/Containers/Data/Application/D17A1BA1-7096-49F9-BDAD-9587B2780715/Documents/54bf3ebf657f6f2c04a81b0b.jpg
"

But when loading this image into a Uiimage it is started with nil, as shown in the code below:

// Implementação de uma UITableView.
...

    NSManagedObject *device = [dadosRetornadosDoCoreData objectAtIndex:indexPath.row];
...
    NSString* stringPath = [device valueForKey:@"imagempath"];
    UIImage* imageNew = [[UIImage alloc]initWithContentsOfFile:stringPath];
    [cell.imagemProduto setImage:imageNew];
    return cell;

I added an image to the project, and uploaded it as the code below:

UIImage* imageNew = [[UIImage imageNamed:@"cloud.png"];

And charged normally.

Detail: the String that leads to the image is coming from the Core Data storage. I don’t know if this can affect the interpretation of the String.

  • Not always the directory Documents will be in the same location, can change in an app update, for example. Ideally you save only the file name and then upload the image by mounting all the added name path.

  • Thanks, I saw this recommendation in another post. How can I locate the folder? type as I tell the app to search for the file with the image name?

1 answer

0


As recommended by friend Paulo Rodrigo, the path of the folders can be changed as updates in the App. It may not be found.

So I made changes only to store the image name in Core Data, and to recover this image, I run the following code:

    NSArray* caminhos = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES);
    NSString* pastaDeDocumentos = [caminhos objectAtIndex:0];
    NSManagedObject *dadosCoreData = [dadosRetornadosDoCoreData objectAtIndex:0];
    NSString* stringPathImage = [NSString stringWithFormat:@"%@/%@",pastaDeDocumentos,[dadosCoreData valueForKey:@"imagempath"]];
    UIImage* imageNew = [UIImage imageWithContentsOfFile:stringPathImage];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.