Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the path for an image in iphone development

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?

like image 339
GeoffreyF67 Avatar asked Dec 10 '22 18:12

GeoffreyF67


1 Answers

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];
like image 144
Jason Coco Avatar answered Dec 22 '22 19:12

Jason Coco