قالب وردپرس درنا توس
Home / IOS Development / ios – Get the same NSData of PHAsset after save

ios – Get the same NSData of PHAsset after save



Do you know there is any way to get the same NSData of the image after saving with PHPhotoLibrary or no?

OfC, iOS will change some metadata and EXIF ​​ data by save, but I'm asking for UIImage Data.

Then 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);
}];

Add the 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) {


Ullmage * 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 I found is if I crop my image to 1 * 1 for testing, I receive some errors ( JPEGDecompressSurface: Image Encoding Failed: ) During storage, it appears that OS can not change image), so I get the same hash before and after save :)!


Source link