How to Compare Two Uiimage Objects

How to compare two UIImage objects

One way is to convert them to image data first, and then compare that.

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

return [data1 isEqual:data2];
}

UIImage is equal to

You can convert your UIImage instances to NSData instances and compare them.

if let emptyImage = UIImage(named: "empty") {
let emptyData = UIImagePNGRepresentation(emptyImage)
let compareImageData = UIImagePNGRepresentation(image1.image)

if let empty = emptyData, compareTo = compareImageData {
if empty.isEqualToData(compareTo) {
// Empty image is the same as image1.image
} else {
// Empty image is not equal to image1.image
}
} else {
// Creating NSData from Images failed
}
}

Compare 2 UIImage objects

If you need to compare images on pixel equality, not pointer, you can do this:

You can create some kind of hash for every image when you create it. For example, the sum of
all pixel values (maybe, modulo some huge number; maybe, powered by pixel position).

Then, store that value in NSDictionary[image] = hashValue. Then, when you compare images, first compare their sizes, as you mentioned, and then, if they are equal, compare their hashes from dictionary.

If they are equal, then images are most possibly equal, but you have to check it manually to be 100% sure.

Manual checking may take some time, so you can decrease the probability of collision (when different images have same hashes) by inventing your own statistics, like MORE HASHES, different modulo, hashes of hashes, value of left-topmost pixel (kidding, but who knows...) and so on.

It is obvious, that the more statistics you have, the less collisions you'll got.
The field of experiments :)

Otherwise, just compare their pointer addresses like this

if(image1 == image2) { 
...
}

How to compare two UIImage in iOS 8

Please compare NSData object instead of comparing UIImage Object.

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

return [data1 isEqualToData:data2];
}

Comparing two UIImages Objective-c

For this functionality, comparing image data uses excessive amount of processor. Instead, you can set button.tag = [image_index] while populating your scrollview.

Than, in your action, you can use button.tag to identify which image is clicked.

- (void)deleteImage:(UIButton*)button { 
int clickedImageIndex = button.tag;
...
[self.imageArray removeObjectAtIndex:clickedImageIndex];
...
}

Comparing UIImage

If you have two UIImages, you should get their CGImageRef quartz representations from those objects. Then create two new bitmap contexts backed by a memory buffer that you create and pass in, one for each of the images. Then use CGContextDrawImage to draw the images into the bitmap contexts. Now the bytes of the images are in the buffers. You can then loop through manually or memcmp to check for differences.

Apple's own detailed explanation and sample code around creating bitmap contexts and drawing into them is here:

https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html

The difference for you is that you're drawing an existing image into the context. Use CGContextDrawImage for this.



Related Topics



Leave a reply



Submit