iOS:如何检查按钮背景图像是什么?

时间:2015-01-31 17:16:05

标签: ios objective-c uibutton conditional-statements

我正在尝试以下方法,但它不起作用。有没有办法可以检查我当前的背景图像是什么用于我的UIButton并进行比较?

UIImage *buttonCheckImage = checkButton.currentBackgroundImage;
UIImage *checkImage = [UIImage imageNamed:@"checkBox.png"];

if (buttonCheckImage == checkImage) {

 }

3 个答案:

答案 0 :(得分:1)

this works using
 [ UIImagePNGRepresentation( uiImage1 ) isEqualToData: 
      UIImagePNGRepresentation( uiImage2 ) ];


UIImage *buttonCheckImage = [self.btn_Checkbox imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:@"checkbox"];
        if([UIImagePNGRepresentation(buttonCheckImage) isEqualToData:UIImagePNGRepresentation(checkImage)])
{
        self.btnImage = [UIImage imageNamed:@"checkbox1"];
    } 
else
 {
        self.btnImage = [UIImage imageNamed:@"checkbox"];
    }
    [self.btn_Checkbox setImage:self.btnImage forState:UIControlStateNormal];

using : http://stackoverflow.com/questions/14078283/comparing-two-images-whether-same-or-not-ios

答案 1 :(得分:0)

希望它的运作良好,试试这个......

·H

@property (weak, nonatomic) IBOutlet UIButton *checkButtonObj;
 -(IBAction)buttonClick:(id)sender;

的.m

-(void)viewDidLoad {
[super viewDidLoad];
[self.checkButtonObj setImage:[UIImage imageNamed:@"correct.png"] forState:UIControlStateNormal];
}

-(IBAction)buttonClick:(id)sender {
if ([self.checkButtonObj.imageView.image isEqual:[UIImage imageNamed:@"correct.png"]])
    [self.checkButtonObj setImage:[UIImage imageNamed:@"wrong.png"] forState:UIControlStateNormal];
else
    [self.checkButtonObj setImage:[UIImage imageNamed:@"correct.png"] forState:UIControlStateNormal]; 
}

答案 2 :(得分:-2)

请尝试以下代码

UIImage *buttonCheckImage = [checkButton imageForState:UIControlStateNormal];
UIImage *checkImage = [UIImage imageNamed:@"checkBox.png"];

if (buttonCheckImage == checkImage) {

}