0

I have a Phonegap app that saves images the user takes from the camera to the device file system. I want to be able to access this image object in iOS/Objective C but can't seem to figure out how and don't see any explanations that solve the issue.

I'm able to see the path to the file (something like "file:///var/mobile/Containers/Data/Application/XXXX-XX-XXXXX/Documents/tempImg.jpg"), and can display this image properly setting HTML img src to this location, but when I try to access the image object in native code something is not working. I've tried using the code below

[UIImage imageWithContentsOfFile:imgName]

where imgName is the path in quotes above, but it does not work. Should I be doing something else?

I've checked all the Stack Overflow answers I could find like the link below, but nothing's working

Load a Saved Image in iOS

Appreciate any help!

Community
  • 1
  • 1
manihiki
  • 682
  • 2
  • 12
  • 22
  • Your "path" in quotes is not a path. It's a file URL. Not the same thing. If you have an `NSURL`, use the `path` method to get a file system path from the file URL. – rmaddy Jan 10 '15 at 04:11
  • Information on the file system on iOS https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html – unobf Jan 10 '15 at 04:22
  • Just consider removing "file:///" from your image URL... and use it with your imageWithContentsOfFile function. @manihiki – Nick Song Jan 10 '15 at 14:10

1 Answers1

5

Try this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filepath = [documentsDirectory stringByAppendingPathComponent: @"tempImg.jpg"];
UIImage myImage = [UIImage imageWithContentsOfFile: filepath];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rudi Angela
  • 1,463
  • 1
  • 12
  • 20