4

Currently the code I am using can write the updated metadata but creates a duplicate image. Here is the code :

if( [self.textView.text length] != 0 && ![self.userComments isEqualToString: self.textView.text])
        {
            // This code works but creates a duplicate image
            NSMutableDictionary *userCommentDictionary = [NSMutableDictionary dictionary];
            [userCommentDictionary setValue:self.textView.text forKey:(NSString *)kCGImagePropertyExifUserComment];

            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setValue:userCommentDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];

            ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];

            [al writeImageToSavedPhotosAlbum:[self.imageView.image CGImage]
                                metadata:dict
                         completionBlock:^(NSURL *assetURL, NSError *error) {
                             if (error == nil) {
                                 NSLog(@"Image saved.");
                                 self.userComments = self.textView.text;
                             } else {
                                 NSLog(@"Error saving image.");
                             }
                         }];
        }

Is there anyway to avoid duplication ? Thanks for your time

userx
  • 1,083
  • 5
  • 18
  • 36
  • 2
    I don't believe this is possible. AssetsLibrary doesn't allow modifying of the original asset at all, everything is saved as a new asset with a reference to the original. With the new PhotoKit library they do, but I do not see anything there that allows you to modify the metadata either. – Jack Jul 18 '14 at 21:38
  • 1
    @JackWu You are correct. Please add as answer. – Léo Natan Jul 18 '14 at 21:46
  • @JackWu, seconded. Do put it in answer form for anyone else who stumbles upon this (plus otherwise half the bounty will go to anyone who takes your comment and paraphrases it in the box below!) – username tbd Jul 18 '14 at 21:48

1 Answers1

7

As noted in the comment, I don't believe this is possible.

AssetsLibrary doesn't allow modifying of the original asset at all, everything is saved as a new asset with a reference to the original.

With the new PhotoKit library in iOS 8 they do allow modifying of the asset, but I do not see anything there that allows you to modify the metadata either.

Taking a glance at ImageIO there are methods to modify metadata, but again, nothing to save it to the photo library. With this, however, you could probably replace a file on disk with another one with modified exif data.

edit to elaborate: According to answers here it seems like ALAssets provide a URL that does not point to the disk. I believe that means that you have no way of getting the acutual URL of the image to overwrite it, though not in the photos library.

I would suggest you file this as an enhancement to Apple if its that important, if many people request the same thing, they might add it in the future! It does seem that they don't want people messing with this stuff though..

Community
  • 1
  • 1
Jack
  • 16,677
  • 8
  • 47
  • 51
  • Thanks for the answer. Will ImageIO let me replace the original image? It would be nice to get a definitive answer on that. – userx Jul 18 '14 at 22:05
  • @AbhishekMukherjee I edited to elaborate a bit. I would say no, there is no way you can modify the metadata of an existing photos library image. – Jack Jul 18 '14 at 22:13
  • I thnk PhotoKit allows you to edit metadata of the original image. Here is some text from Apple Docs " You use the photo library object to perform changes to Photos objects, such as editing asset metadata or content, inserting new assets, or rearranging the members of a collection." – userx Jul 23 '14 at 14:56
  • You can trust me when I say it doesn't allow it. By "metadata" they mean stuff like `dateCreated`, `location`, `isFavorite`, `isHidden`. That's actually about it. You can dig into the API and see for yourself. – Jack Jul 23 '14 at 15:07
  • Oh , I thought metadata would mean all the metaData of the image. Thanks for the info. – userx Jul 23 '14 at 18:45