gpt4 book ai didi

iphone - 从 NSData 在 iPhone 上计算校验和

转载 作者:行者123 更新时间:2023-12-03 18:21:24 24 4
gpt4 key购买 nike

使用 iPhone SDK,我让用户从图像选择器中选择图像。如果用户选择了他们之前选择过的图像,我想让用户意识到这一点。

我最初的计划(只是为了确保其他事情现在有效)是将图像保存到文件中(出于其他原因无论如何都需要这样做),使用 NSData 的校验和作为文件名。然后,当他们稍后选择相同的图像时,校验和将相同,因此我可以看到具有该名称的文件已经存在 - 欢呼!

但是,我已经在互联网和 Apple 文档中搜索了如何从 NSData 计算校验和。我知道我可以实现自己的实现,但如果可能的话,我宁愿避免这种情况。我也很高兴了解如何检查两个 UIImage 是否相同的其他想法。

编辑

两年前,我 promise 提供一个代码示例,现在就在这里。真的很抱歉耽误了! :)

+(NSString*)imageIdForData:(NSData*)data
{
char* result = (char*) [[data MD5Sum] bytes];

NSString* hash = [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]];

return hash;
}

最佳答案

因为有了类别一切都会变得更好......

标题:

@interface NSData (MD5)
- (NSString *)md5String;
@end

实现:

#import <CommonCrypto/CommonDigest.h>


- (NSString *)md5String
{
void *cData = malloc([self length]);
unsigned char resultCString[16];
[self getBytes:cData length:[self length]];

CC_MD5(cData, (unsigned int)[self length], resultCString);
free(cData);

NSString *result = [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
resultCString[0], resultCString[1], resultCString[2], resultCString[3],
resultCString[4], resultCString[5], resultCString[6], resultCString[7],
resultCString[8], resultCString[9], resultCString[10], resultCString[11],
resultCString[12], resultCString[13], resultCString[14], resultCString[15]
];
return result;
}

关于iphone - 从 NSData 在 iPhone 上计算校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1028742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com