imageWithContentsOfFile Not Returning Image
ios, objective-c, uiimage
Solution
Not sure if the issue I was having is the same as yours but we were suddenly having this issue where we were getting nil for our images when using the full file path and using the imageWithContentsOfFilePath method.
We found after a bit of debugging that the NSHomeDirectory() (which is basically your sandbox location), now changes on every launch. Our issue was that we were storing the entire file path and on subsequent launches, trying to retrieve them from that same full file path, which no longer existed.
For example, on one launch your home directory might be: /var/mobile/Containers/Data/Application/844B1DDA-49AB-4B5C-AC76-2BBF1397B142 but it would be different on the next: /var/mobile/Containers/Data/Application/C620B610-3839-4134-9DF7-C5756F22BBCE. Therefore, if you store file paths for future use, you'll have to determine the home directory portion of the file path at the time you require it and simply store the relative path of the file/image.
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [self retrieveHomeDirectoryPath], relativeFileLocationPath];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
Here retrieveHomeDirectoryPath would simply return the result of NSHomeDirectory() or NSTemporaryDirectory() and relativeFileLocationPath would be the path relative to that specific location.
Problem
UIImage *image=[UIImage imageWithContentsOfFile:filePath]; I have used this method a hundred times before and it used to always work, but now it just returns" Null", I googled and I tried with ``` NSBundle *bundle = [NSBundle mainBundle]; NSString *filePath = [bundle pathForResource: @"fileName" ofType:@"png"]; UIImage *image=[UIImage imageWithContentsOfFile:filePath]; ``` But its still not Working, Also tried using UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]]; still get "Null", And I can't use the Method ``` UIImage *image=[UIImage imageNamed:@"image.png"]; ``` Since I am downloading the Images inside my Apps Document Directory inside a "tmp" folder and then using the path where I have downloaded them and then trying to access it, Hence I need this method "imageWithContentsOfFile:" to work desperately can somebody please help me out of this…Thanks for help in advance