我正在尝试比较文件系统中的两个 UIImage,看看它们是否相同。显然,我不能使用 NSObject 的哈希方法,因为它返回对象的哈希,而不是实际的图像数据。
我发现代码从字符串生成 MD5 哈希值,但我还没有发现如何为 UIImage 实现它。
我应该如何对 UIImage 进行哈希处理?还是我与图像进行比较的方法完全错误?
最佳答案
我最终使用以下代码来完成任务。请注意,这需要您导入 <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]
];
关于iphone - 从 UIImage 生成哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1684799/