views:

32

answers:

2

I have an array of NSStrings:

Flower
Car
Tree
Cat
Shoe

Some of these strings have images associated with them; some don't. I can build an image name by appending .png to the name (e.g. Flower.png).

How do I check whether that image actually exists within the bundle before I try to load it into the view?

+1  A: 

Just load the resource and check whether it is nil or not:

NSString* myImagePath = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"jpg"];
if (myImagePath != nil) {
    // Do something with image...
}
Steve Harrison
+1  A: 

This should also work, and is a bit shorter:

if (flowerImage = [UIImage imageNamed:@"Flower.png"])
{
   ... // do something here
}
else
{   
   ... // image does not exist
}

The imageNamed: method will look in your main bundle for the png file.

Emil