Reputation: 8003
I have this problem... I need to load an image from the resources of my app that is called for example mystuff01.jpg but maybe be called mystuff01.gif or png, now, what's the best solution to do this? In my mind there's a cycle to retrieve if the file exists and if exists load it... there's a better solution? thanks
Upvotes: 0
Views: 993
Reputation: 11161
If you are loading the file from the file system and not the bundle, then you might want to use this:
NSString *assetsDirectory = [....] // your base asset directory, e.g. documents directory
NSArray *extensions = [NSArray arrayWithObjects:@"png", @"jpg", @"jpeg", nil];
NSFileManager *fm = [NSFileManager defaultManager];
NSString *storageLocation = nil;
for (NSString *ext in extensions) {
NSString *testLocation = [NSString stringWithFormat:@"%@/mystuff.%@", assetsDirectory, ext];
if ([fm fileExistsAtPath:testLocation]) {
storageLocation = testLocation;
break;
}
}
// file name is in "storageLocation" now
Upvotes: 2
Reputation: 53689
From UIImage
On iOS 4 and later, the name of the file is not required to specify the filename extension. Prior to iOS 4, you must specify the filename extension.
Otherwise, you would have to try each extension. You could pass the name with each extension directly to [UIImage imageNamed:]
, find one that works with [mainBundle pathForResource:ofType:];
, or use NSFileManager to get a list of resources and look for the closest match.
Upvotes: 2
Reputation: 18741
I think one of the solutions is to create an NSArray {@".jpg", @".png" }, loop through the array and add the extension to the file name, then check if the file name + extension exists, then you load it. It will be easier to do, and can be done with loop
Upvotes: 1