Get the same NSData of PHAsset after save

585 Views Asked by At

Do you know is there any way to get the same NSData of Image ( JPG , PNG ) after save with PHPhotoLibrary or no?

OfC, iOS will modify some metadata and EXIF-- > ( Timestamp,... )data after save but, I'm asking about UIImage Data (include same EXIF data). I didn't copy the exif in in my code here but it doesn't work so Let's talk over the code:

Save Image and get hash

 UIImage * tmp = [[UIImage alloc] initWithData:tmpData];
 tmpData =UIImageJPEGRepresentation(tmp, 1.0);
 self.str1 =  [tmpData MD5];                
 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
 PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
 options.originalFilename = @"XXX";
 PHAssetCreationRequest * createReq   =   [PHAssetCreationRequest creationRequestForAsset];
[createReq  addResourceWithType:PHAssetResourceTypePhoto data:tmpData options:options];
            } completionHandler:^(BOOL success, NSError * _Nullable error) {
    NSLog(@":%d",success);
 }];

Load same Image :

[asset requestContentEditingInputWithOptions:0 completionHandler:^(PHContentEditingInput * _Nullable contentEditingInput,  NSDictionary * _Nonnull info) {
 PHImageRequestOptions * option = [[PHImageRequestOptions alloc] init];
 option.synchronous = YES;
 option.version = PHImageRequestOptionsVersionOriginal;
 option.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
 option.resizeMode = PHImageRequestOptionsResizeModeNone;
 [[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {


 UIImage * image = [UIImage imageWithData:imageData];
 NSData * tmpDAt =  UIImageJPEGRepresentation(image, 1.0);           
 NSString * md5 =  [tmpDAt MD5];
 if ([md5 isEqualToString:self.str1]) {
  NSLog(@"My Expextation");
  }   
}];

The Intresting thing that I found is if I crop my image to 1*1 for test, I receive some error ( JPEGDecompressSurface : Picture decode failed: ) during save (It seems OS can't modify image) so I get the same hash before and after save :) !

2

There are 2 best solutions below

2
On

I presume the difference is due to your JPEGs having different timestamps (and possibly other differences) in their EXIF metadata.

Have you tried using UIImagePNGRepresentation instead of UIImageJPEGRepresentation? Hopefully PNG representations will match.

0
On

Jpeg compression is a Lossy form of compression. Every time you convert to Jpeg you will lose data. There is no way around it. Removing PHPhotoLibrary from the equation. If you run the following

UIImage * tmp = [[UIImage alloc] initWithData:tmpData];
tmpData =UIImageJPEGRepresentation(tmp, 1.0);
str1 =  [tmpData MD5];
tmp = [[UIImage alloc] initWithData:tmpData];
tmpData =UIImageJPEGRepresentation(tmp, 1.0);
str2 =  [tmpData MD5];

You will find that str1 and str2 are different.

If you want the same data you will have to either keep the original jpeg data that generated the image or use a loseless compression method like the one used within the PNG files.