0

I am trying to find the full path for a file called 'file1.jpg'

I have verified that it is in the app bundle and yet when I run this code:

NSBundle* myBundle = [NSBundle mainBundle];     

NSString* path = [myBundle pathForResource:@"file1" ofType:@"jpg"]; 

'path' doesn't have the path/file I'm expecting.

This is code directly from the Apple documentation.

What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
GeoffreyF67
  • 11,061
  • 11
  • 46
  • 56
  • 1
    What is the value of `path`? What were you expecting? Can you show a file tree for the contents of the app bundle? – Shaggy Frog Dec 12 '09 at 03:16
  • 1
    He is trying to make an image. Geoffrey, just use imageNamed: and be done with it. If either of those return nil, it means that your file does not exist. If imageNamed: returns nil and you are 100% sure the file exists (i.e., pathForResource:ofType: returns something other than nil), it means that your image is not in a valid format. – Jason Coco Dec 12 '09 at 03:18
  • This works: [files addObject: [UIImage imageNamed:@"file1.jpg"]]; But my filename is dynamic (file1.jpg, file2.jpg and so on). – GeoffreyF67 Dec 12 '09 at 03:23
  • The dynamic file will work with imageNamed too. If you do something like int num = 1; NSString* file = [NSString stringWithFormat:@"file%d.jpg", num]; UIImage* image = [UIImage imageNamed:file]; and the file 'file1.jpg' exists exactly like that and is a valid image file, it will work too. If it doesn't exist exactly as spelled or is not a valid image format, it will fail and return nil. – Jason Coco Dec 12 '09 at 03:26
  • So this: NSString *file = [NSString stringWithFormat:@"file%d.jpg",count]; should show up in the debugger as something other than 'invalid' - right? This is weird... – GeoffreyF67 Dec 12 '09 at 03:40
  • I mean the value for 'file' would show up as somethijng other than 'invalid' – GeoffreyF67 Dec 12 '09 at 03:40
  • Yes, as long as count is an integer, file will be something other than invalid. However, if you are in the debugger, you will have to step past that line *before* file will show up as something as invalid. So if your breakpoint is *on* that line, you must step one or you will see file as invalid. – Jason Coco Dec 12 '09 at 04:06

2 Answers2

3

If path is nil then file1.jpg does not exist in your application bundle. Make sure that it is in your project and that it is set to be included in your current target. Also, it is case sensitive, so make sure that the file is really named file1.jpg and not file1.JPG or file1.jpeg or something.

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
0

It also helps if you are building the DEBUG version and not the RELEASE version when using the debugger :)

GeoffreyF67
  • 11,061
  • 11
  • 46
  • 56