UIImage from file problem
Solution 1
You need to point to the Documents directory within your app then like this:
- (NSString *)applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
Usage:
UIImage *img = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/70.jpg",[self applicationDocumentsDirectory]]];
Solution 2
First, you are using pathForResource wrong, the correct way would be:
[[NSBundle mainBundle] pathForResource:@"70" ofType:@"jpg"]
The whole idea of bundling is to abstract the resource path such as that it will always be valid, no matter where in the system your app resides. But if all you want to do is load that image I would recommend you use imageNamed: since it automatically handles retina resolution (high resolution) display detection on the iPhone for you and loads the appropriate resource "automagically":
UIImage *img = [UIImage imageNamed:@"70.jpg"];
To easily support regular and retina resolution you would need to have two resources in your app bundle, 70.jpg and [email protected] with the @2x resource having both doubled with and height.
Solution 3
Try loading a UIImage with:
[UIImage imageNamed:@"something.png"]
It looks for an image with the specified name in the application’s main bundle. Also nice: It automatically chooses the Retina ([email protected]) or non-Retina (xyz.png) version.
Solution 4
Your path simply wont work because your app is in a sandbox, and you are trying to use the full path.
You should be using the following instead:
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"70" ofType:@"jpg"]];
or you can use, but is slower than the above:
UIImage *img = [UIImage imageNamed:@"70.jpg"];
Phil
Updated on July 30, 2022Comments
-
Phil almost 2 years
I am trying to load an saved image but when I check the UIImage it comes back as nil. Here is the code:
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"/var/mobile/Applications/B74FDA2B-5B8C-40AC-863C-4030AA85534B/Documents/70.jpg" ofType:nil]];
I then check img to see if it is nil and it is. Listing the directory shows the file, what am I doing wrong?
-
Phil about 13 yearsThanks for the response, I tried both of these and still am getting nil when I do: if (img == nil) {NSLog(@"is nil");} I am starting to wonder if I am saving it wrong.
-
Phil about 13 yearsI can see in my Documents directory the "70.jpg" exists. Is that what you mean, I am a little confused with "your bundle". Thank you again.
-
WrightsCS about 13 yearsCheck your project files, and you can also open the .app file to make sure the resource was copied over. Most likely the file is not included in your project, if it was, then the sample I provided would work.
-
Phil about 13 yearsI am capturing the image from the camera and saving it to the Documents directory.
-
WrightsCS about 13 yearsThe issue with your solution is that the
UIImage
is looking for the jpg in the app bundle, where-as he needs it from the App's Documents directory. -
KClough about 12 yearsI was having an issue with retina images and this solved my problem. Many thanks.