if then Statements with images

Discussion in 'iOS Programming' started by Programmer, Sep 23, 2009.

  1. Programmer macrumors member


    Jun 16, 2009
    I am creating a game for the iphone and i have frame by frame images set up of my object getting closer and closer to you (I used frame by frame so i could speed up and slow down the object without distortion). Is there a way to say that when the UIImageView equals a certian to do something. I tried saying this.

    if (Main.image = [UIImage imageNamed:mad:"picture1.png"]) {

    //My Code Was Here...

    I put that in the view did load and when the app started up the image was set to that image instead of the NSArray of images it was supposed to go through.
  2. tcarcur macrumors newbie

    Jan 16, 2008
    On an if statement, to check for equalities you must use "==" rather than "=".

    The way you have it, you are telling it to make the image of Main to the UIImage you want to compare, rather than actually comparing.

    I don't know if if statements work with images, but this code should work:

    if (Main.image == [UIImage imageNamed:@"picture1.png"]) {
  3. robbieduncan Moderator emeritus


    Jul 24, 2002
    That won't work. You are checking that the memory address of the images is the same. And it isn't.

    Basically unless you track which object came from which file yourself you cannot do what you are trying to do: UIImage does not retain this information and you cannot ask it to check if one image is the same as another (and this would be a potentially very expensive operation if you could).
  4. Programmer thread starter macrumors member


    Jun 16, 2009
  5. drf1229 macrumors regular


    Jun 22, 2009
    It goes like this:

    == means is equal to
    != means is not equal to
    >= means is greater than or equal to
    <= means is less than or equal to
    < means less than
    > means greater than

    You can also handle multiple booleans at once:

    if (var2+var1==var3 && var3+2==var4){
    //These statements will only work if both conditions are true
    if (var2+var1==var3 || var3+2=var4){
    //These statements will work if just one condition is true
    //It does not matter which one is true
    //You can add as many || or && symbols as you want
    If I'm missing anything, please tell me
    I'm not sure if it works with images but I know it works with Image views
  6. North Bronson macrumors 6502

    Oct 31, 2007
    San José
    Since you are dealing with pointers to objects, you want something like:

    if ([firstImage isEqualToImage: secondImage])

    which doesn't exist.

    What about:

    NSData *firstImageData = UIImagePNGRepresentation(firstImage);

    NSData *secondImageData = UIImagePNGRepresentation(secondImage);

    if ([firstImageData isEqualToData: secondImageData])

    This is certainly *not* the optimal way to go about doing things. You should have some kind of mapping (maybe even just a C array).

Share This Page