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 objec
more elegant code below
+(NSString *)MD5HexDigest:(NSData *)input {
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(input.bytes, (unsigned int)input.length, result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for (int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02x",result[i]];
}
return ret;
}
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).
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]
];
Swift Code for hashing a UIImage with the SHA256 algorithm in swift 4.2. There are other algorithms you can use, which are may faster or generate less duplicates but this one is good enough for most usecases.
Just drop the code somewhere in your Project and use it like this: yourUIImage.sha256()
extension UIImage{
public func sha256() -> String{
if let imageData = cgImage?.dataProvider?.data as? Data {
return hexStringFromData(input: digest(input: imageData as NSData))
}
return ""
}
private func digest(input : NSData) -> NSData {
let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
var hash = [UInt8](repeating: 0, count: digestLength)
CC_SHA256(input.bytes, UInt32(input.length), &hash)
return NSData(bytes: hash, length: digestLength)
}
private func hexStringFromData(input: NSData) -> String {
var bytes = [UInt8](repeating: 0, count: input.length)
input.getBytes(&bytes, length: input.length)
var hexString = ""
for byte in bytes {
hexString += String(format:"%02x", UInt8(byte))
}
return hexString
}
}
By the way you need a Bridging Header that imports CommonCrypto. If you don't have one follow these steps:
BridgingHeader
ProjectName/BridgingHeader.h
#import <CommonCrypto/CommonHMAC.h>
in your Header FileThe duplicate probability is low, but you can upgrade to SHA512 relatively easy if you got huge datasets:
The Duplicate Probability is about:
Source: https://crypto.stackexchange.com/questions/24732/probability-of-sha256-collisions-for-certain-amount-of-hashed-values