iphone - 从 UIImage 生成哈希值

标签 iphone cocoa-touch hash

我正在尝试比较文件系统中的两个 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/

相关文章:

iphone - ios:通过 iPhone/iPad 代码打开/关闭飞行模式

ios - viewForAnnotation 详细披露更改当前位置以及引脚

c# - 我应该如何在 C# 中计算文件哈希 (md5 & SHA1)

arrays - 迭代散列,并取值直到达到某个总和限制

ios - 如何在不解包可选值的情况下使用字符串变量

iphone - 如何将 uiviewcontroller 推到 tabbarcontroller 上

iphone - 在iPhone上将OpenGL调用与更新分开

ios 将锁屏通知类型表单横幅更改为警报类型

c++ - 字符串的哈希函数不适用于某些字符串?

iphone - 为 UIScrollView 实现自定义缩放