I want to save an image with some metadata changes in a temp folder, without re-encoding the actual image data.
The only method that I found able to do this is ALAssetsLibrary/writeImageDataToSavedPhotosAlbum:metadata:completionBlock:, however, this one saves the image to the Photo Library. Instead, I want to save the image to a temp folder (for instance to share it by email, without populating the Photo Library).
I've tried using CGImageDestinationRef
(CGImageDestinationAddImageFromSource), but it can only be created using a decoded image, which means it's re-encoding it when saved (tested, pixel bytes look different).
Are there any other methods/classes available for iOS that can save image data along with metadata, besides using CGImageDestinationRef
? Suggestions for workarounds would also be welcomed.
Alongside iphone-exif, and Aaron's answer, you might also want to look at the libexif c library.
see also HCO23's answer to
How to write or modify EXIF data for an existing image on the filesystem, without loading the image?
(@HCO23 has used libexif in iOS projects).
It's also worth noting that many of the 'professional' metadata editing apps in the app store seem to skirt around the issue by creating sidecar/xmp files.
This is a frustrating problem with the iOS SDK. First, I recommend filing an enhancement request.
Now, here is a potential workaround: IF the ALAsset was created by you (i.e., its
editable
property isYES
) then you can basically read the data, write with metadata, read again, save to disk, and then write with the original metadata.This approach will avoid creating a duplicate image.
Please read the // comments as I skipped some stuff for brevity (like building a metadata dictionary):
As you can see, if the ALAsset isn't owned by you, you will need to create one, which will add a photo to the user's library, which is exactly what you wanted to avoid. However, as you may have guessed, you cannot delete an ALAsset from the user's photo library, even if your app created it. (Feel free to file another enhancement request for that.)
So, if the photo/image was created in your app, this will work for you.
But if not, it will create an additional copy the user must delete.
The only alternative is to parse the NSData yourself, which would be a pain. There is no open-source library that I am aware of to fill this gap in the iOS SDK.
Try This :