Objective-C: comparing an image with another previously saved image

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

The goal of what I want is to direct the user to a new screen with a click, depending on what the image displays.

For simplicity, we say that there are two possibilities - a black image and a green image. Clicking on the black image will lead you to xib1, and clicking on the green image will lead you to xib2.

It is quite simple and works 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 for download 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 normal, and when I exit the program and restart it, the image is saved on the screen as I would like. Hypothetically suppose that the image that appears on button 1 is a green image.

When I call the following code after clicking the button with the sender ID (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]; } 

although currentImage is a green image and is the same image as the green image on which I am comparing it, I think because I saved the green image in memory during saving, the comparison does not work, because they are held in different places in the memory - verified by the following NSLog:

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

I can’t figure out how to compare the two images so that in this case they match (both refer to the same image that I have in the resources folder). Does anyone have any suggestions?

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

Thanks in advance!

+8
ios memory objective-c compare uiimage
source share
2 answers

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

In addition, the problem is that you use the == operator to compare the memory addresses of images 0x95614c0 and 0xde748f0. That is why it is not equal. You compare if they are the same object, and not if the images are equal.

To compare the use of images: As mentioned in Fls'Zen's answer.

 if ([UIImagePNGRepresentation(blackImage) isEqualToData:UIImagePNGRepresentation(greenImage)]) 
+25
source share

Your images, of course, have different addresses, since one of them is downloaded from your application package, and one of them is downloaded from the document catalog. The [UIImage imageNamed:] function returns only images from the application package.

If you really want to compare images by content, check out this SO question. The first answer computes the value of the hash function s for the image. In your code, you can compare the hash values ​​of the two images that you have. The second answer compares the images directly, in case the hashes make you nervous.

I recommend that you follow a different route and have your own application track, the image of which is loaded outside the image itself.

+4
source share

All Articles