Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate hash from UIImage

Tags:

I'm trying to compare two UIImages from the file system to see if they are the same. Obviously, I can't use NSObject's hash method, since this returns a hash of the object, and not the actual image data.

I found code generate an MD5 hash from a string, but I haven't discovered how to implement it for a UIImage.

How should I go about hashing a UIImage? Or is my method for comparing to images totally off?

like image 458
Reed Olsen Avatar asked Nov 06 '09 01:11

Reed Olsen


People also ask

How to use online hash generator tool?

Use of Online Hash Generator Tool. After you have generated hash data, you can simply click on "Copy to Clipboard" or select all converted text and press "Control-C" to copy, and then "Control-V" to paste it back into your document. Alternatively you can download generated hash data to text file simple click on the "Download" button.

How to download generated hash data to text file?

Alternatively you can download generated hash data to text file simple click on the "Download" button You can upload data text file to generated various hash data. If you like this tool and helpful to your work, then please recommend it to you friends and family who would also find it useful.

What are the methods of uiimage in UIKit?

Other methods of the UIImage class let you create animations from specific types of data, such as Core Graphics images or image data you create yourself. UIKit also provides the UIGraphicsGetImageFromCurrentImageContext () function to create images from content you draw yourself.

What is the use of uiimage class?

You use image objects to represent image data of all kinds, and the UIImage class is capable of managing data for all image formats supported by the underlying platform. Image objects are immutable, so you always create them from existing image data, such as an image file on disk or programmatically created image data.


2 Answers

I wound up using the following code to accomplish the task. Note that this requires that you import <CommonCrypto/CommonDigest.h>:

unsigned char result[CC_MD5_DIGEST_LENGTH]; 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]                        ]; 
like image 89
Reed Olsen Avatar answered Oct 31 '22 11:10

Reed Olsen


A less than optimal solution:

[ UIImagePNGRepresentation( uiImage1 ) isEqualToData:        UIImagePNGRepresentation( uiImage2 ) ]; 

This basically compares the PNG encoded data of the 2 images. Since image similarity is a complex subject, better and faster solutions can be devised based on what exactly the end goal is (i.e. are you looking to compare images, pixel by pixel, or just approximate similarity, which could use a downsampled version of the source image, etc).

like image 45
codelogic Avatar answered Oct 31 '22 12:10

codelogic