I have this snippet of objective c code:
UIImage *image = [ [UIImage alloc] initWithContentsOfFile:fileName];
fileName is set to "file1.jpg"
When I run the code, however, image is set to nil.
I know the file does exist I am guessing it has something to do with the path.
What path should I be using?
The easiest thing to use is imageNamed:
like this:
UIImage* theImage = [UIImage imageNamed:@"file1.jpg"];
If you need to use initWithContentsOfFile:
for some reason, you need to get the path from the bundle, like this:
NSString* path = [[NSBundle mainBundle] pathForResource:@"file1" ofType:@"jpg"];
UIImage* theImage = [[UIImage alloc] initWithContentsOfFile:path];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With