从 UIImage 生成哈希 [英] Generate hash from UIImage

查看:32
本文介绍了从 UIImage 生成哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较文件系统中的两个 UIImage 以查看它们是否相同.显然,我不能使用 NSObject 的 hash 方法,因为它返回的是对象的散列,而不是实际的图像数据.

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.

我发现代码从字符串生成 MD5 哈希,但我还没有发现如何为 UIImage 实现它.

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

我应该如何散列 UIImage?或者我的对比图像的方法完全不可行?

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

推荐答案

我最终使用以下代码来完成任务.请注意,这需要您导入 :

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]
                       ];

这篇关于从 UIImage 生成哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆