ios - NSData dataWithContentsOfFile 在设备上返回不同的结果

标签 ios iphone hash md5 nsdata

我在 PNG 图像上使用 dataWithContentsOfFile,iOS 模拟器返回的值与 iOS 设备不同。我的设备是 64 位 iPad Air (iOS 8.3),我的模拟器设置为 iPad Retina iOS 8.3。这是我正在使用的代码:

    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
    NSData *data = [NSData dataWithContentsOfFile:sourcePath];
    NSString *hash = [self md5Hash:data]

    // Results on device: 2D25F346396FB00BEB27754ED1B56310
    // Results on simulator: 55016FD1AB3DA0F882FEA85D5ABCA2ED

我用一个字符串测试了我的散列函数,无论设备如何,它都能正常工作。我不会显示 dataWithContentsOfFile 方法的结果,但我可以向您保证它们是不同的。

更新:测试 dataWithContentsOfURL 工作正常,示例:

    NSURL *fileURL = [NSURL URLWithString:@"https://www.google.com/images/srpr/logo11w.png"];
    NSData *data = [NSData dataWithContentsOfURL:fileURL];
    NSString *hash = [self md5Hash:data]

    // Results on device: 57E396BAEDFE1A034590339082B9ABCE
    // Results on simulator: 57E396BAEDFE1A034590339082B9ABCE

最佳答案

我猜它是 Xcode 将 PNGCrush 应用到您的图像...所以它实际上不是同一个文件。

关于ios - NSData dataWithContentsOfFile 在设备上返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30769195/

相关文章:

iOS:自动布局:当有足够的空间时,两个按钮并排,否则彼此重叠

iphone - 检测 UIWebView 内的点击

iphone - 关闭或离开 View Controller 时如何将数据插入 sqlite3 数据库?

java - hash()%n 和 n%hash() 有什么区别

Java加密与解密

ios - UITableViewCell backgroundView 绘制矩形未被调用

android - 在 Android、iOS 和 Blackberry 上嵌入字体——是否有万无一失的解决方案?

IOS:NSString 的发布未按预期工作

ios - 重新加载 UITableView 时显示隐藏的 UIButton

java - Android 中提供哪些哈希算法?