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

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

问题描述

我试图比较两个UIImages从文件系统,看看他们是否相同。显然,我不能使用NSObject的哈希方法,因为这将返回对象的哈希,而不是实际的图像数据。

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?

推荐答案

我结束了使用下面的代码来完成任务。请注意,这需要您导入< CommonCrypto / CommonDigest.h>

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天全站免登陆