18

Here is what I'm doing, when I create an image with the path in the bundle:


UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image" ofType:@"jpg"]];

What I want to do is trying to find the path for my image but without using the extension, without using 'ofType' (because the name of my image and her extension is store in my database) something like that:


UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image.jpg"]];

But I don't know how to do it.

Best regards,

Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
ludo
  • 1,479
  • 5
  • 25
  • 50

5 Answers5

41

Why don't you split the string that you get from the DB?

NSString* fullFileName = @"image.jpg";
NSString* fileName = [[fullFileName lastPathComponent] stringByDeletingPathExtension];
NSString* extension = [fullFileName pathExtension];

Now you can simply use:

[[NSBundle mainBundle] pathForResource:fileName ofType:extension]];
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • 2
    You don't need to separate the extension. See my answer. I don't know if there is any performance impact by using my approach however. – Brody Robertson Feb 10 '15 at 21:58
25

You can easily use the NSBundle method without passing the extension, just pass nil for extension.

[[NSBundle mainBundle] pathForResource:@"image.jpg" ofType:nil];
Brody Robertson
  • 8,506
  • 2
  • 47
  • 42
  • 2
    This should be the excepted answer IMHO as it's more elegant and to the point. I'm not sure why apple insists on separating the extension at all. – TalL Oct 10 '17 at 16:20
2
- (NSData *)applicationDataFromFile:(NSString *)fileName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
    return myData;
}

taken from http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html#//apple_ref/doc/uid/TP40007072-CH21-SW21

Could be easily adapted if you wanted it to return a UIImage instead of an NSData.

Also, you don't say if you are saving the images to the documents directory, or adding them to your app bundle before compiling. because if it's the latter, you can use [UIImage imageNamed:(NSString *)filename] to get the image. It expects an extension as part of the file-name.

Kenny Winker
  • 11,919
  • 7
  • 56
  • 78
0

The easiest way is to store the name and file type in your database separately, and retrieve them using the first method.I'm not sure that you can be successful in implementing the latter one.

Nithin
  • 6,435
  • 5
  • 43
  • 56
  • I was thinkinf of that, but creating a column in my database just for extension I don't know if its the best way – ludo Dec 31 '09 at 09:07
0

I found that with an extension of ".jpg" it was necessary to use ofType for the extension for the app to work on an iPod Touch, whereas with an extension of ".png" I could just put "image.png" in pathForResource and say ofType:nil. But all versions worked on the simulator.

The app bundle contains the image file, and I am using:

[[NSBundle mainBundle] pathForResource:@"Auto" ofType:@"jpg"]

to get a path.

Jonathan Starr
  • 236
  • 4
  • 15