Two ways to compare UIImages show different results. Who to believe?

I am 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");

As a result of YES, THEY ARE EQUAL

if i do the following

` if ([holderImage isEqual:[UIImage imageNamed:@"empty_image.png"]])
            NSLog(@"empty image");
        else
            NSLog(@"not empty image"); `

no result, they are NOT

The situation is rather complicated because:

1) Images SHOULD BE (this means I'm pretty sure) equal, so I would believe the first if

2) isEqual comparison always gives true results in other images.

So, I'm completely confused. what do you think about it? Btw the ownerImage was just taken from NSUserDefaults. Do you think this can be changed in some way when stored in NSUserDefaults, so now isEqual compares?

+5
source share
2 answers

isEqual UIImage / , , isEqual NSData , .

isEqual, , . Apple , NSData isEqual -.

, , .

+7

PivotalCoreKit helper . , (, [UIImage -UIImageNamed] [UIImage initWithData:], 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;
}
+4

All Articles