Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 422
user1309044 Avatar asked Jun 26 '12 21:06

user1309044


2 Answers

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)])
like image 127
Paul N Avatar answered Nov 15 '22 20:11

Paul N


Your images certainly have different addresses since one is loaded from your application bundle and one is loaded from the documents directory. The [UIImage imageNamed:] function only returns images from the application bundle.

If you really want to compare the images by contents, check out this SO question. In the first answer, a hash value s computed for an image. In your code, you could compare the hash values of the two images you have. The second answer compares the images directly, in case hashes make you nervous.

I recommend going a different route and having your application track which image is loaded outside of the image itself.

like image 25
Fls'Zen Avatar answered Nov 15 '22 21:11

Fls'Zen