Iphone 图像比较在ios模拟器中不起作用
我正在开发一个应用程序。因为我正在对下面的两幅图像进行比较操作Iphone 图像比较在ios模拟器中不起作用,iphone,ios,uiimageview,Iphone,Ios,Uiimageview,我正在开发一个应用程序。因为我正在对下面的两幅图像进行比较操作 UIImage *actual_image=usrcheck_image.image; NSData *present_image = UIImagePNGRepresentation(actual_image); NSData *compare_image = UIImagePNGRepresentation([UIImage imageNamed:@"unchk-1.png"]); if([present_image isE
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.
}
如果我运行此操作,则“检查图像”将更改为“取消检查”。但“取消检查图像”不会更改为“检查图像”。每次执行else块时都会执行。因此,请帮助我如何比较这两个图像。此代码在设备中运行得很好。但问题仅存在于模拟器中。您可以使用
UIImageView
的突出显示的属性。将选中的图像设置为highlightedImage
属性,然后单击事件集:
yourImageView.isHighlighed = !yourImageView.isHighlighed;
它将自动显示您的图像作为切换可能下面的链接可以帮助您
让我知道这是否有效请尝试以下代码-
拍摄两张选中和未选中的图像,并将该图像分配给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];
在按钮功能中-
selectedBtn.selected = NO;
button.selected = YES;
selectedBtn = button;
在.h文件中,仅提及
UIButton *selectedBtn;
希望对您有所帮助。首先,不需要比较图像数据,也不需要比较图像
但如果你想比较图片的意思,只需像下面这样做
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"];
}
看这个,比较图像数据只是为了改变复选框的状态(大概)是一个非常糟糕的主意,性能方面。那么我如何更改imageview的图像。基本问题:为什么需要执行图像比较?这稍微好一些。但是我必须将图像更改为正常状态而不是选定状态。好的。但是我可以知道为什么需要执行图像比较吗?让我知道为什么需要它,以便我尝试解决您的问题。:)@用户1498119:你对这个问题有意见吗?不要只使用一个图像视图。将其图像属性设置为未选定图像。并将其高亮显示属性设置为选定图像。当你们点击按钮时,使用你们的ImageView.isHighlighed=!yourImageView.isHighlighed;仅此代码,无需执行其他操作此代码也与我使用的相同。它在设备上工作,但在模拟器中不工作。它在模拟器中不工作。可能是在设备上工作。我需要在模拟器中执行此操作。如果您的图像出现在项目源中。。。当然它也会在模拟器中工作。。。