我如何比较 UIButton backgroundImage 标题?

发布于 2024-10-29 04:31:40 字数 465 浏览 7 评论 0原文

在 UIButton 上,我设置像这样的背景图像

[twitterButton setBackgroundImage:twitterImage forState:UIControlStateNormal];

twitterImage 的类型为 UIImage

现在,在单击任何按钮的代码中的某处,我使用标签值设置 twitterButton 图像

[twitterButton setBackgroundImage:twitterImage1 forState:UIControlStateNormal];

我的问题是如何比较 twitterButton 的图像?

这种情况不起作用 if([twitterbutton.imageView.image isEqual:twitterImage1])

请帮忙

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([twitterbutton.imageView.image isEqual:twitterImage1])

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软糯酥胸 2024-11-05 04:31:40

尝试与 backgroundImageForState: 方法返回的 UIImage 进行比较,而不是 UIButton imageView 的图像属性...

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

即,

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'
}

请参阅 UIButton 类参考了解更多信息。

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.

夜唯美灬不弃 2024-11-05 04:31:40

您无法比较UIButton的backgroundImage。您使用一些引用变量

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

好倦 2024-11-05 04:31:40

尝试使用 UIButton 的 currentBackgroundImage 属性。这将为您提供当前的背景图像。 UIButton参考

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

苯莒 2024-11-05 04:31:40

我认为这很昂贵,但您可以尝试将图像转换为 nsdata,然后使用 isEqualToData。

编辑

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

if([data1 isEqualToData:data2]){

}

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]){

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文