5

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?

4 Answers 4

18

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.

2
  • It is good convention to use isEqual: when checking if two objects are equal. You could also use == but I would not recommend as in certain situations it will not return the expected results (sometimes when comparing objects and their parameters). The current code is also wrong as you are assigning flower image instead of checking if it is equal to the image png. Copying this code will throw up an error which adding isEqual: or == will fix.
    – sam_smith
    Jul 8, 2014 at 0:18
  • This works but an iOS alert message appears saying that an image could not be found.
    – Herno
    May 10, 2016 at 15:02
6

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...
}
3

I would assign the image as a variable, then you can check if the image exists:

UIImage * flowerImage = [UIImage imageNamed:@"Flower.png"];

if (flowerImage) {
    // Do something
}
else {
    // Do something else
}

In the accepted answer we are checking to see if the picture equals an image with a specific name. This could go wrong as we will return no if

  • The image Flower.png doesn't exist

AND

  • The image Flower.png does exist but flowerImage is set to a different image meaning they are not equal

Assigning a variable to the image and then checking if the variable is assigned or not will return us a definite yes or no depending on whether Flower.png exists

4
  • 1
    Dot point 2 is not true. The assignment operator in an if statement has exactly the same effect as an assignment before the if statement. The code is equivalent to the accepted answer code.
    – Ephemera
    Jul 8, 2014 at 1:56
  • Ok. But my answer is running under the assumption that the accepted answer will error. If == is substituted instead of = then if Flower.png doesn't exist or if it is different to flowerImage then we will get the image doesn't exist. Maybe the accepted answer should have: ((flowerImage = [UIImage imageNamed:@"Flower.png"])) as this assigns it too (stackoverflow.com/questions/3323257/…). In this case you are right that point two will not be a factor. Without editing though, it will error.
    – sam_smith
    Jul 8, 2014 at 2:05
  • Using = inside an if condition only throws a warning, and I don't think it even does that anymore (at least not in Xcode, you could set a compiler flag for it though). In any case, this is one of that other 10% that the accepted answer to that question talks about. The result of an assignment expression is the left hand side of the expression (the lvalue - flowerImage in this case). If that is nil (i.e. -imageNamed: returned nil) then the if condition will be false. Otherwise it will be true.
    – Ephemera
    Jul 8, 2014 at 4:39
  • I think we are both arguing the same point. Both methods work but the accepted answer could raise problems if not understood properly, hence why I posted an answer which approaches the problem from a different way to help people in case they get stuck. Thanks for commenting though as it has made me research and understand this more and will also help people understand the differences of each method and complications that can occur.
    – sam_smith
    Jul 8, 2014 at 4:58
0

I don't even add the ".png" to the file name and it works:

NSArray *images = @[@"Flower", @"Car", @"Tree", @"Cat", @"Shoe"];

if ([UIImage imageNamed:images[0]]){
        // there is a flower image
    } else {
        // no flowers for you
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.