NSData dataWithContentsOfFile returns 设备上的不同结果

NSData dataWithContentsOfFile returns different results on device

我在 PNG 图像和 iOS 模拟器上使用 dataWithContentsOfFile returns 一个不同于 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 应用到您的图像...所以它实际上不是同一个文件。