0

I am developing one application.In that i am doing comparision operation for two images like below

 UIImage *actual_image=usrcheck_image.image;
NSData *present_image = UIImagePNGRepresentation(actual_image);
NSData *compare_image = UIImagePNGRepresentation([UIImage imageNamed:@"unchk-1.png"]);

if([present_image isEqualToData:compare_image])
{
   set the checked image
}
else
{
   set the uncheck image.
 }

If i run this,check image is changed to uncheck.But uncheck image is not chneged to check image.Everytime else block is executed.So please help me how to compare these two images.This code is working perfectly in device.But the problem is in simulator only.

4

4 回答 4

1

试试这个代码 -

拍摄 2 张​​选中和未选中的图像,并将这些图像分配给 UIButton。

 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 160, 30);
[button setImage:[UIImage imageNamed:@"checkedImage.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"uncheckedImage.png"] forState:UIControlStateSelected];
button.tag = 1;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

而在 buttonAction 函数中 -

selectedBtn.selected = NO;
button.selected = YES;
selectedBtn = button;

在 .h 文件中只需提及

UIButton *selectedBtn; 

希望它可以帮助你。

于 2013-06-27T10:31:25.323 回答
0

可能是下面的链接可以帮助你

Objective-C:将图像与之前保存的另一张图像进行比较

让我知道这个是否奏效

于 2013-06-27T10:30:34.623 回答
0

首先,不需要比较图像数据,也不需要比较图像,

但是,如果您想比较图像的方式,只需执行以下操作即可

if(usrcheck_image.image==[UIImage imageNamed:@"unchk-1.png"]) {
    usrcheck_image.image=[UIImage imageNamed:@"chk-1.png"];
} else {
    usrcheck_image.image=[UIImage imageNamed:@"unchk-1.png"];
}
于 2013-06-27T10:40:30.823 回答
0

您可以使用highlighted. UIImageView将选中的图像设置为highlightedImage属性,并在点击事件集上:

yourImageView.isHighlighed = !yourImageView.isHighlighed;

它会自动将您的图像显示为切换

于 2013-06-27T10:29:02.433 回答