jthemovie jthemovie - 1 year ago 186
Objective-C Question

UUImage imageWithContentsOfFile is nill

I'm trying to load a picture from Media/DCIM folder in iOS 8 in order to compress them on the fly later on, so I started writing this script.

int main(int argc, char **argv, char **envp) {

NSString *str = @"Media/DCIM/101APPLE/IMG_1022.JPG";

NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:str];

NSLog(@"File not found at given path");
//NSString *path = [[NSBundle mainBundle] pathForResource:str ofType:nil];
//UIImage* image = [UIImage imageWithContentsOfFile:path];
UIImage* image = [UIImage imageWithContentsOfFile:str];
CIImage *cim = [image CIImage];

if (cim == nil)
NSLog(@"File found at given path, but failing to allocate it as an UUImage obj");
NSLog(@"All good !");
[image release];
return 0;

The output gives "File found at given path, but failing to allocate it as a UUImage obj."

Any ideas why the file is found, but impossible to load it as a UUImage type?
I also tried loading the path as NSBundle, but still, no lucks.

Thanks a lot in advance

Answer Source

Simply you can't access the Media/DCIM folder in iOS because the system is sandboxed. According to the documentation, your app can read and write files directly only on these subfolders of your application folder:

  • Documents/
  • Documents/Inbox/
  • Library/
  • tmp/

For other purposes like photos, videos and songs picking you need to use the designated pickers of iOS.

Since you need to pick an image from the camera roll, you should use an UIImagePickerController object instead. It returns the proper UIImage for the picture chosen in the picker controller.

Take a look to this article from NSHipster for more information about NSFileManager. Also read the content of this question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download