I'm trying to compare two UIImages. If i compare it like this:
if ([UIImagePNGRepresentation ( holderImage) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:@"empty_image.png"])])
NSLog(@"empty image");
else
NSLog(@"not empty image");
the result is YES, THEY'RE EQUAL
if i'm doing the following
` if ([holderImage isEqual:[UIImage imageNamed:@"empty_image.png"]])
NSLog(@"empty image");
else
NSLog(@"not empty image"); `
the result is NO, THEY'RE NOT
The situation is pretty complicated because:
1) Images MUST BE (it means i'm pretty much sure)equal so i would believe the first one unless
2) isEqual comparison always gives true result on other images.
So i'm completely confused. What do you think about that? Btw the holderImage was just taken from NSUserDefaults. Do you think it might be changed somehow while being stored in NSUserDefaults so that isEqual comparison is lying now?
The isEqual
method on UIImage
looks a the pointer/hash of the object where as the isEqual
method on NSData
will look it the bytes are the same.
The isEqual
used by most object are based on hash. In the Apple documentation it is specified that NSData
implements the isEqual
method in a different way.
Two data objects are equal if they hold the same number of bytes, and if the bytes at the same position in the objects are the same.
PivotalCoreKit provides a helper for comparing the images via bytes. This will pass if the images are created via different sources (eg. [UIImage -UIImageNamed]
and [UIImage initWithData:]
, unlike UIImagePNGRepresentation()
.
- (BOOL)isEqualToByBytes:(UIImage *)otherImage {
NSData *imagePixelsData = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage));
NSData *otherImagePixelsData = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(otherImage.CGImage));
BOOL comparison = [imagePixelsData isEqualToData:otherImagePixelsData];
CGDataProviderRelease((CGDataProviderRef)imagePixelsData);
CGDataProviderRelease((CGDataProviderRef)otherImagePixelsData);
return comparison;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With