Reputation: 21726
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.
Upvotes: 1
Views: 2400
Reputation: 5925
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) {
...
}
Upvotes: 6