NSData dataWithContentsOfFile returns different results on device

479 views Asked by At

I'm using dataWithContentsOfFile on a PNG image and the iOS Simulator returns a value that differs from an iOS Device. My device is a 64-bit iPad Air (iOS 8.3) and my simulator is set to iPad Retina iOS 8.3. Here's the code that I'm using:

    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

I tested my hash function with a string, and it works fine regardless of device. I'm not going to display the results of the dataWithContentsOfFile method, but I can assure you that they're different.

Update: Testing dataWithContentsOfURL works fine, example:

    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
1

There are 1 answers

1
cbiggin On BEST ANSWER

I'm going to guess that it's Xcode applying PNGCrush to your image... so it is actually not the same file.