Search code examples
objective-cmacosxcode7nsdatansimage

OS X: Comparing two images


I'm trying to implement a way to compare two images but I'm testing my and comparing the same image to make sure is working but doesn't work. here is may code:

NSImage *file = [[NSImage alloc] initWithContentsOfFile:path];
NSData *imgDataOne = [file TIFFRepresentation];
NSData *imgDataTwo = [file TIFFRepresentation];

if (imgDataOne == imgDataTwo)
{
    NSLog(@"is the same image");
}

The if is never true. Any of you knows what I'm doing wrong or if another way to compare the images?

I'll really appreciate your help.


Solution

  • TIFFRepresentation will return a new NSData object. Comparing these objects using the == operator will always return false because these are two different objects.

    NSData has isEqualToData method to test if these two NSData objects contain the same binary data.