Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
878 views
in Technique[技术] by (71.8m points)

ios - Objective-C: Comparing an image against another image that has been previously saved

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.

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];

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.

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];
}

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!

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...