How can I compare the name of the UIButton backgroundImage?

On UIButton, I set a background image like this

[twitterButton setBackgroundImage:twitterImage forState:UIControlStateNormal]; 

twitterImage is of type UIImage

Now somewhere in the code on any button I clicked, I set the twitterButton image using the tag value

 [twitterButton setBackgroundImage:twitterImage1 forState:UIControlStateNormal]; 

My problem: how would I compare the twitterButton image?

this condition does not work if([twitterbutton.imageView.image isEqual:twitterImage1])

Please, help

+7
source share
4 answers

Try comparing with the UIImage returned by the backgroundImageForState method: instead of the image property of the UIButton ...

 if ([[twitterButton backgroundImageForState:UIControlStateNormal] isEqual:twitterImage1]) 

t.,

 if ([[twitterButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed@ "myImage.png"]]){ // Button has a background image named 'myImage.png' }else{ // Button has not a background image named 'myImage.png' } 

See the UIButton class reference for more information.

+10
source

you cannot compare backgroundImage to UIButton's.you set backgroundImage with some reference variable

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html

+2
source

Try using the currentBackgroundImage property for UIButton. This will give you the current background image. UIButton Help

+1
source

I think this is expensive, but you can try converting the images to nsdata and then use isEqualToData.

Edit

 NSData *data1=UIImagePNGRepresentation([twitterButton backgroundImageForState:UIControlStateNormal]); NSData *data2=UIImagePNGRepresentation(anotherUIImage); if([data1 isEqualToData:data2]){ } 
0
source

All Articles