0

I have two image array, where I want to compare if the images are same in both the arrays, how to compare two UIImage objects..??

for (int i=0; i< imageArray_1.count; i++) {
        for (int j=0; j< imageArray_2.count; j++) {
            if ([[imageArray_1 objectAtIndex:i]isEqualToData:[imageArray_2 objectAtIndex:j]])  {
                NSLog(@"Matched");
            }
            else{
                NSLog(@"Not Matched");
            }
        }
    }

It does not work for me, Any suggestions will be appreciated. Thanks in advance.

Bappaditya
  • 9,494
  • 3
  • 20
  • 29

3 Answers3

1

There is no need to convert image and then compare it every time. You should use hash from crypto framework. From example:

unsigned char result[16];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(inImage)];
CC_MD5([imageData bytes], [imageData length], result);
NSString *imageHash = [NSString stringWithFormat:
                       @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                       result[0], result[1], result[2], result[3], 
                       result[4], result[5], result[6], result[7],
                       result[8], result[9], result[10], result[11],
                       result[12], result[13], result[14], result[15]
                       ];

EDIT

(This code also came from SO somewhere but i'm using in in my app, i can't link original question ;) )

Jakub
  • 13,712
  • 17
  • 82
  • 139
1

This worked for me:

NSData *freeImageData=[NSData dataWithContentsOfFile:@"free.png"];
NSData *noImageData=[NSData dataWithContentsOfFile:@"no.png"];
if([[NSData noImageData] isEqual:freeImageData])
{
     ....
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
mak
  • 1,173
  • 1
  • 12
  • 18
0

Use this method:

- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
{
    NSData *data1 = UIImagePNGRepresentation(image1);
    NSData *data2 = UIImagePNGRepresentation(image2);

    return [data1 isEqual:data2];
}

Answered here:

How to compare two UIImage objects

pkamb
  • 33,281
  • 23
  • 160
  • 191
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • 3
    If you copy/paste an answer, please link to the original http://stackoverflow.com/questions/11342897/how-to-compare-two-uiimage-objects – Mick MacCallum Nov 15 '13 at 12:30