views:

485

answers:

3

Hi,

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,

A: 

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
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
+6  A: 

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]];
weichsel
I will try that as soon as I get home I think. thank you~
ludo
+1  A: 
- (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%5Fref/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
The file is adding before the compilation. thanks for the answer
ludo