开发者

How would I compare UIButton backgroundImage title?

On a UIButton I set background images like this

[twitterButton setBackgroundImage:twitterImage forState:UIControlStateNormal];

twitterImage is of type UIImage

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

[twitterButton setBackgroundImage:twitterImage1 forState:UIControlStateNormal];

My problem is How would I compare the image of twitterButton ?

this condition is not working if([twitte开发者_如何学编程rbutton.imageView.image isEqual:twitterImage1])

Please help


Try comparing against the UIImage returned by the backgroundImageForState: method instead of the UIButton imageView's image property...

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

ie.,

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.


you can't compare backgroundImage of UIButton's.you set the backgroundImage using some reference variable

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


Try using the currentBackgroundImage property of UIButton. This will give you the current background image.. UIButton Reference


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

Edit

NSData *data1=UIImagePNGRepresentation([twitterButton backgroundImageForState:UIControlStateNormal]);
NSData *data2=UIImagePNGRepresentation(anotherUIImage);

if([data1 isEqualToData:data2]){

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜