1

I have read a lot of questions and answers here on stackowerflow about the solution and still can't find the solution which can help to solve my problem.

So i have 2 really big images and i need to compare them.

Images are not created with imageNamed:, so they are not cashed, so [image1 isEqual:image2] should not work.

The only solution to compare them as i understand is this one:

- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2 { return [UIImagePNGRepresentation(image1) isEqual:UIImagePNGRepresentation(image2)]; } 

But the images have really huge sizes, so it will take a some time to compare.

Are there any properties which can help not to use method above to check if they are equal?

For example i can get image1.size and compare it with image2.size, it they are not equal i do not need to run method above. Any ideas? Thank you.

9
  • Whether or not they are cached does not matter if you already have them in memory. While I haven't tried it, I don't see any reason that isEqual: shouldn't work. Have you tried it? Commented Mar 13, 2013 at 15:39
  • How about creating a custom object by inheriting from UIImage and extend it with a property e. g. NSString *uniqueID for identification? Commented Mar 13, 2013 at 15:41
  • @Inafziger : I tried isEqual: , didn't help Commented Mar 13, 2013 at 15:42
  • Are you retrieving the images over the network or from the local file system/bundle? Commented Mar 13, 2013 at 15:49
  • UIPasteBoard, that is why @iDroid idea doesn't match, because images from pasteboard would not have my uniqueId, so i think i should calculate some hashes. Commented Mar 13, 2013 at 15:53

1 Answer 1

6

If you need to compare images on pixel equality, not pointer, you can do this:

You can create some kind of hash for every image when you create it. For example, the sum of all pixel values (maybe, modulo some huge number; maybe, powered by pixel position).

Then, store that value in NSDictionary[image] = hashValue. Then, when you compare images, first compare their sizes, as you mentioned, and then, if they are equal, compare their hashes from dictionary.

If they are equal, then images are most possibly equal, but you have to check it manually to be 100% sure.

Manual checking may take some time, so you can decrease the probability of collision (when different images have same hashes) by inventing your own statistics, like MORE HASHES, different modulo, hashes of hashes, value of left-topmost pixel (kidding, but who knows...) and so on.

It is obvious, that the more statistics you have, the less collisions you'll got. The field of experiments :)

Otherwise, just compare their pointer addresses like this

if(image1 == image2) { ... } 
Sign up to request clarification or add additional context in comments.

3 Comments

if(image1 == image2) also do not work in my case, but i like the idea with some hashing
Still i have the same problem, to hash hug image and check it it will also take some time, so i can hash some properties or part of the image
You are welcome, updated the answer with some info about collisions and stuff :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.