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.

B.S.
  • 21,660
  • 14
  • 87
  • 109
  • 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? – lnafziger Mar 13 '13 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? – iDroid Mar 13 '13 at 15:41
  • @Inafziger : I tried isEqual: , didn't help – B.S. Mar 13 '13 at 15:42
  • Are you retrieving the images over the network or from the local file system/bundle? – Elliott Mar 13 '13 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. – B.S. Mar 13 '13 at 15:53
  • Do you need to compare them very often (with different images of course)? How big are they? What dimenstions are we talking about? – Hermann Klecker Mar 13 '13 at 16:03
  • What about drawing them into some off-screen context of limited space and compare the results? – Hermann Klecker Mar 13 '13 at 16:04
  • @Hermann Klecker: What dimensions? Any dimensions you can imagine, it's pasteboard, there can be more than 20 Mpixels images on new devices. – B.S. Mar 13 '13 at 16:26
  • possible duplicate of [Cocoa Touch - Comparing Images](http://stackoverflow.com/questions/3400707/cocoa-touch-comparing-images) – hpique May 18 '14 at 18:01

1 Answers1

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) { 
     ...
}
dreamzor
  • 5,795
  • 4
  • 41
  • 61
  • if(image1 == image2) also do not work in my case, but i like the idea with some hashing – B.S. Mar 13 '13 at 15:47
  • 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 – B.S. Mar 13 '13 at 15:48
  • You are welcome, updated the answer with some info about collisions and stuff :) – dreamzor Mar 13 '13 at 16:10