Get the same NSData of PHAsset after save












4















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 :) !










share|improve this question





























    4















    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 :) !










    share|improve this question



























      4












      4








      4


      1






      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 :) !










      share|improve this question
















      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 :) !







      ios objective-c phasset phphotolibrary






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 21:13







      Mo Farhand

















      asked Nov 19 '18 at 11:58









      Mo FarhandMo Farhand

      784519




      784519
























          2 Answers
          2






          active

          oldest

          votes


















          0














          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.






          share|improve this answer
























          • I was thinking about this too but what If I copy all of EXIF data for the new image ? (JPEG)

            – Mo Farhand
            Nov 21 '18 at 18:44











          • also, I tried with UIImagePNGRepresentation also as you know PNG doesn't keep the orientation so I check the md5 results in all orientation and doesn't match with the original one. I think it can be related to timestamp and ... but not sure as I'm copy all of EXIF information for the new image

            – Mo Farhand
            Nov 21 '18 at 18:51











          • any idea? have you tried the same?

            – Mo Farhand
            Nov 23 '18 at 12:50



















          0














          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.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374159%2fget-the-same-nsdata-of-phasset-after-save%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            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.






            share|improve this answer
























            • I was thinking about this too but what If I copy all of EXIF data for the new image ? (JPEG)

              – Mo Farhand
              Nov 21 '18 at 18:44











            • also, I tried with UIImagePNGRepresentation also as you know PNG doesn't keep the orientation so I check the md5 results in all orientation and doesn't match with the original one. I think it can be related to timestamp and ... but not sure as I'm copy all of EXIF information for the new image

              – Mo Farhand
              Nov 21 '18 at 18:51











            • any idea? have you tried the same?

              – Mo Farhand
              Nov 23 '18 at 12:50
















            0














            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.






            share|improve this answer
























            • I was thinking about this too but what If I copy all of EXIF data for the new image ? (JPEG)

              – Mo Farhand
              Nov 21 '18 at 18:44











            • also, I tried with UIImagePNGRepresentation also as you know PNG doesn't keep the orientation so I check the md5 results in all orientation and doesn't match with the original one. I think it can be related to timestamp and ... but not sure as I'm copy all of EXIF information for the new image

              – Mo Farhand
              Nov 21 '18 at 18:51











            • any idea? have you tried the same?

              – Mo Farhand
              Nov 23 '18 at 12:50














            0












            0








            0







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 15:39









            ClafouClafou

            12.2k54580




            12.2k54580













            • I was thinking about this too but what If I copy all of EXIF data for the new image ? (JPEG)

              – Mo Farhand
              Nov 21 '18 at 18:44











            • also, I tried with UIImagePNGRepresentation also as you know PNG doesn't keep the orientation so I check the md5 results in all orientation and doesn't match with the original one. I think it can be related to timestamp and ... but not sure as I'm copy all of EXIF information for the new image

              – Mo Farhand
              Nov 21 '18 at 18:51











            • any idea? have you tried the same?

              – Mo Farhand
              Nov 23 '18 at 12:50



















            • I was thinking about this too but what If I copy all of EXIF data for the new image ? (JPEG)

              – Mo Farhand
              Nov 21 '18 at 18:44











            • also, I tried with UIImagePNGRepresentation also as you know PNG doesn't keep the orientation so I check the md5 results in all orientation and doesn't match with the original one. I think it can be related to timestamp and ... but not sure as I'm copy all of EXIF information for the new image

              – Mo Farhand
              Nov 21 '18 at 18:51











            • any idea? have you tried the same?

              – Mo Farhand
              Nov 23 '18 at 12:50

















            I was thinking about this too but what If I copy all of EXIF data for the new image ? (JPEG)

            – Mo Farhand
            Nov 21 '18 at 18:44





            I was thinking about this too but what If I copy all of EXIF data for the new image ? (JPEG)

            – Mo Farhand
            Nov 21 '18 at 18:44













            also, I tried with UIImagePNGRepresentation also as you know PNG doesn't keep the orientation so I check the md5 results in all orientation and doesn't match with the original one. I think it can be related to timestamp and ... but not sure as I'm copy all of EXIF information for the new image

            – Mo Farhand
            Nov 21 '18 at 18:51





            also, I tried with UIImagePNGRepresentation also as you know PNG doesn't keep the orientation so I check the md5 results in all orientation and doesn't match with the original one. I think it can be related to timestamp and ... but not sure as I'm copy all of EXIF information for the new image

            – Mo Farhand
            Nov 21 '18 at 18:51













            any idea? have you tried the same?

            – Mo Farhand
            Nov 23 '18 at 12:50





            any idea? have you tried the same?

            – Mo Farhand
            Nov 23 '18 at 12:50













            0














            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.






            share|improve this answer




























              0














              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.






              share|improve this answer


























                0












                0








                0







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 11 '18 at 10:21









                SpadsSpads

                1,6261519




                1,6261519






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374159%2fget-the-same-nsdata-of-phasset-after-save%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents