Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify image metadata ( EXIF ) in iOS without duplicating?

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

like image 251
userx Avatar asked Mar 19 '23 02:03

userx


1 Answers

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..

like image 197
Jack Avatar answered Apr 24 '23 23:04

Jack