Objective-C:将图像与先前保存的另一图像进行比较 [英] Objective-C: Comparing an image against another image that has been previously saved

查看:97
本文介绍了Objective-C:将图像与先前保存的另一图像进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,当一个图像已经通过保存和加载过程使用NSSearchPathForDirectoriesInDomains方法时,在Objective-C中比较UIImages。

I have a question about comparing UIImages in Objective-C when one image has already been through a save and load process using the NSSearchPathForDirectoriesInDomains method.

我想要的是根据图像显示的内容,在用户点击一个新的屏幕。

The aim of what I want is to direct the user to a new screen upon a click depending on what the image displays.

为简单起见,我们假设有两种可能性 - 黑色图像和绿色图像。点击黑色图片,您将进入xib1并点击绿色图片,您将进入xib2。

For simplicity let's say that there are two possibilities - a black image and a green image. Clicking on the black image takes you to xib1 and clicking the green image takes you to xib2.

这很简单,一直工作,直到我实现了一个保存和加载系统。

This is simple enough and has been working until I have implemented a save and load system.

要保存,请执行以下操作:

To save I do the following:

paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
pngFilePath = [NSString stringWithFormat:@"%@/test.png",documentsDirectory];
data1 = [NSData dataWithData:UIImagePNGRepresentation([level1part1 objectAtIndex:0])];
[data1 writeToFile:pngFilePath atomically:YES];

并加载我执行以下操作:

and to load I do the following:

paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
pngFilePath = [NSString stringWithFormat:@"%@/test.png",documentsDirectory];
UIImage *image = [UIImage imageWithContentsOfFile:pngFilePath];
[button1 setImage:image forState:UIControlStateNormal];

这很好,当我退出程序并重新启动时,图像保留在屏幕上希望它。假设我们说现在出现在button1上的图片是绿色图片。

This is fine and when I quit the program and restart it the image is retained on the screen as I wish it to be. Hypothetically let's say that image now appearing on button1 is the green image.

当我点击发件人ID为button1的按钮后调用下面的代码

When I call the following code after clicking on the button with id of sender (this is button1):

if(sender.currentImage == [UIImage imageNamed:self.greenImage])
{
    VisitAlreadyCorrectScreen *screen = [[VisitAlreadyCorrectScreen alloc] initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES];
}

即使currentImage是绿色图像, image我比较它,我想因为我在保存过程中将绿色图像保存到内存比较不工作,因为他们在不同的地方在内存中保存 - 通过以下NSLog验证:

even though the currentImage is the green image and is the same picture as the green image I am comparing it to, I think because I saved the green image into memory in the save process the comparison doesn't work as they are held in different places in the memory - verified by the following NSLog:

Current Image: <UIImage: 0x95614c0>, and Yes Image: <UIImage: 0xde748f0>

我无法弄清楚如何比较两个图片,因此在这种情况下他们匹配到我在资源文件夹中的同一图像)。有人有任何建议吗?

I cannot work out how to compare the two images so that in this case they match (they both relate to the same image I have in the resource folder). Does anyone have any suggestions at all?

请让我知道,如果我没有解释得很好的问题是什么!

Please let me know if I have not explained well enough what the problem is!

提前感谢!

推荐答案

您可以比较图片名称或图片网址,它也会比比较图像更快。

You can compare the image name or the image URL if it was downloaded from Internet, it will also be faster than comparing the images.

此外,问题是,通过使用==运算符,你正在比较图像0x95614c0和0xde748f0的内存地址。这就是为什么不相等。

Also, the problem is that by using the == operator you are comparing the memory addresses of the images 0x95614c0 and 0xde748f0. That's why is not equal. You are comparing if they are the same object, not if the images are equal.

要比较图片使用:如Fls'Zen答案所述。

To compare images use: As mentioned on Fls'Zen answer.

if ([UIImagePNGRepresentation(blackImage) isEqualToData:UIImagePNGRepresentation(greenImage)])

这篇关于Objective-C:将图像与先前保存的另一图像进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆