Search code examples
iosnsbundleapp-bundle

iOS - check if image is present in app bundle


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?


Solution

  • 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];
    }