【问题标题】:NSData dataWithContentsOfFile returns different results on deviceNSData dataWithContentsOfFile 在设备上返回不同的结果
【发布时间】:2015-06-10 23:44:20
【问题描述】:

我在 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

【问题讨论】:

  • 我想说 MD5 的不同是因为不同的架构使用不同的数字格式。会不会是可调整大小的 iPad 以 32 位运行?可能相关:stackoverflow.com/questions/697361/…
  • 对不起,我弄错了。我使用的是 64 位的 Retina iPad。我已经针对文本字符串测试了哈希码,在我的情况下,它们在模拟器和设备上给出了相同的结果。
  • 我猜是文件不一样。
  • (考虑当你请求一个图像文件时,文件系统会根据显示分辨率选择几个同义词之一。)

标签: ios iphone hash md5 nsdata


【解决方案1】:

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

【讨论】:

  • 就是这样,在设备上构建 pngcrush 操作图像。我必须将 pngcrush 指令设置为 NO 才能正常工作。有趣的是,对于模拟器构建,pngcrush 不会应用于图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
相关资源
最近更新 更多