How can I tell if an image is present in the main app bundle based on a string? For example, is there an image called image1.png in the bundle?
Asked
Active
Viewed 375 times
1 Answers
2
NSBundle method:
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension
will return nil if no image was found.
i.e.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image1" ofType@"png"];
if(filePath.length > 0 && filePath != (id)[NSNull null]) {
UIImage *myImage = [UIImage imageWithContentsOfFile:filePath];
}

ohr
- 1,717
- 14
- 15
-
Thanks. How do I get the image if I find it, since that method just returns a string? – soleil Sep 12 '12 at 18:38
-
[UIImage imageWithContentOfFile:yourFilePath]; – J2theC Sep 12 '12 at 18:44
-
The second comparison in the if statement is incorrect. Nil is not the same thing as [NSNull null], so the comparison doesn't make sense. – Anton Sep 12 '12 at 21:32