0

I am trying to compare an image from a button with another image but its not working as expected and as stackoverflow suggestions...

if (favoriteButton.imageView.image  == [UIImage imageNamed:@"favorite.png"]) {
    NSLog(@"yes!");
}

anything else I can do? I tried to get the image file name but it seems to be impossible.

Andre Cytryn
  • 2,506
  • 4
  • 28
  • 43

5 Answers5

0

UIImage is a class, not a native type. You should use the isEqual: method, not the == operator to see if the two objects represent the same data.

if ([favoriteButton.imageView.image isEqual:[UIImage imageNamed:@"favorite.png"]]) {
    NSLog(@"yes!");
}

Only use the == operator with object pointers if you really want to see if the two pointer reference the exact same location in memory.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I guess `UIImage` doesn't actually implement an `isEqual:` method that properly compares two images. Once solution would be to create `NSData` from the images (using `UIImagePNGRepresentation`) and then comparing the two `NSData` objects. – rmaddy Apr 26 '13 at 18:06
  • 1
    @AndréCytryn - I believe `-isEqual:` with UIImage does a pointer comparison, like you're trying above. `-imageNamed:` may or may not return a new UIImage instance when called (there is some caching done behind the scenes). Pointer comparison is not going to work reliably for you here. – Brad Larson Apr 26 '13 at 18:06
  • This is not working when I first time click the button.After first time clicking the button the condition is working. – user3182143 Apr 27 '16 at 07:39
  • I set the background image in identity inspector for button.Initially I set the set the background image for button as checkbox_off_background.png. – user3182143 Apr 27 '16 at 07:41
  • When I click the button,it doesn't come into the if condition.It comes to else part directly.But once I click the button I mean next time(second time) when I click the button it comes into if condition.What is the reason?Why doesn't directly take the image from identity inspector of xib or stroyboard? – user3182143 Apr 27 '16 at 07:46
  • This is my coding - if([btnCheckList.currentBackgroundImage isEqual:[UIImage imageNamed:@"checkbox_off_background.png"]]) { [btnCheckList setBackgroundImage:[UIImage imageNamed:@"checkbox_on_background.png"] forState:UIControlStateNormal]; } else { [btnCheckList setBackgroundImage:[UIImage imageNamed:@"checkbox_off_background.png"] forState:UIControlStateNormal]; } – user3182143 Apr 27 '16 at 07:46
  • @user3182143 Please post your own question. Adding a bunch of unrelated comments to an answer is not the best way to get help. After posting your own question with all of the relevant details, please delete all of the comments here. Thanks. – rmaddy Apr 27 '16 at 13:54
0

I found this: How to compare two UIImage objects

this is what I tink may help:

- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2

    NSData *data1 = UIImagePNGRepresentation(image1);
    NSData *data2 = UIImagePNGRepresentation(image2);

    return [data1 isEqual:data2];
}
Community
  • 1
  • 1
iOSGuru248
  • 386
  • 2
  • 6
  • how about the answer from this: [Cocoa Touch - Comparing Images](http://stackoverflow.com/questions/3400707/cocoa-touch-comparing-images) – iOSGuru248 Apr 26 '13 at 19:00
0

Reading the comments of your question, i think u cloud just assign the button a "state" such as "selected".

On the normal state the button cloud load the "not_favorite.png".If the user tap on the button it will be on a selected state and the corresponding image(favorite.png) will load.

On future if you need to know which image is loaded(or if the user already taped on the button to make it favorite) u can just check the button's state.

Personally i will not compare images just to fire an action for a button. It will be costly.

Shad
  • 1,587
  • 2
  • 20
  • 29
0

why not try to use imageView to identify the image?

UIImageView *imageView_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"favorite.png"]];
UIImageView *imageView_not_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"not_favorite.png"]];

//both imageView_favorite and imageView_not_favorite can be set as an member ivar, and the favoriteButton.imageView can be set to them appropriately.

if (favoriteButton.imageView  == imageView_favorite) {
    NSLog(@"yes!");
}
lu yuan
  • 7,207
  • 9
  • 44
  • 78
0

try this

must check your buttonname and extension. it must be same.

    -(IBAction)btnMaleClick:(id)sender { 
        if (btnMale.imageView.image == [UIImage imageNamed:@"radiobtn.png"]) {
            [btnMale setImage:[UIImage imageNamed:@"radiobtnselected.png"] forState:UIControlStateNormal];
            [Female setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
        }
        else {
            [btnMale setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal];
            [Female setImage:[UIImage imageNamed:@"radiobtnselected.png"] forState:UIControlStateNormal];
        }
    }
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45