5

Possible Duplicate:
How does one compare one image to another to see if they are similar by a certain percentage, on the iPhone?

I've found this code and am trying to understand it better:

UIImage *img1 = // Some photo;
UIImage *img2 = // Some photo;

NSData *imgdata1 = UIImagePNGRepresentation(img1);

NSData *imgdata2 = UIImagePNGRepresentation(img2);

if ([imgdata1 isEqualToData:imgdata2]) {
    NSLog(@"Same Image");
}

Will this confirm that image 1 is exactly the same as image 2? Is this method best practice, or is there a better approach to this?

Community
  • 1
  • 1
PeshZ
  • 91
  • 1
  • 2
  • 5
  • 5
    "Figure 1 design .. Start writing your first app" <- ?? –  Dec 29 '12 at 03:01
  • You should really fix your question. If you are quoting from a book, state it clearly and don't make non-sense references to `Figure 1`, etc – Gabriele Petronella Dec 29 '12 at 08:11
  • This kind of comparison is really useful when you have set image using [UIImage imageWithContentsOfFile:YOUR_IMAGE_FILE_PATH] instead of [UIImage imageNamed:@"YOUR_IMAGE_NAME">] because if you have set image using file path and trying to compare its not works so at that time this kind of comparison is useful. – Sandip Patel - SM Feb 12 '13 at 06:40
  • Actually, this is a duplicate of http://stackoverflow.com/questions/3400707/cocoa-touch-comparing-images/23725088#23725088, not the current duplicate. – hpique May 18 '14 at 18:00

2 Answers2

5

Your code is comparing the two images bit by bit, so yes it's a 100%-comparison.

If you need something faster you can generate an hash from each UIImage and compare the two hashes, as explained here.

Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • This kind of comparison is really useful when you have set image using [UIImage imageWithContentsOfFile:YOUR_IMAGE_FILE_PATH] instead of [UIImage imageNamed:@"YOUR_IMAGE_NAME">] because if you have set image using file path and trying to compare its not works so at that time this kind of comparison is useful. – Sandip Patel - SM Feb 12 '13 at 06:44
1

Take a look at this link, it talks all about sampling to images to see the percentage similarity: How does one compare one image to another to see if they are similar by a certain percentage, on the iPhone?

Community
  • 1
  • 1
Yo_Its_Az
  • 2,043
  • 2
  • 18
  • 25