I'm trying to use the metadata property in writeImageDataToSavedPhotosAlbum
to save a GIF to the iPhone's photo album and add date/time metadata so that the saved GIF appears in a different place in the user's photo album instead of the default location at the end of the photo album.
I've tried doing the following:
let metadata: [String: AnyObject]! = [kCGImagePropertyTIFFDictionary as String:
[kCGImagePropertyTIFFDateTime as String: dateTime!],
kCGImagePropertyExifDictionary as String:
[kCGImagePropertyExifDateTimeDigitized as String: dateTime!,
kCGImagePropertyExifDateTimeOriginal as String: dateTime!]]
library.writeImageDataToSavedPhotosAlbum(data, metadata: metadata, completionBlock: completionBlock)
and the debug print for the metadata variable shows:
["{TIFF}": {
DateTime = "2015:10:09 20:07:48";
}, "{Exif}": {
DateTimeDigitized = "2015:10:09 20:07:48";
DateTimeOriginal = "2015:10:09 20:07:48";
}]
However, it doesn't seem like setting the metadata worked because the GIF is still saved at the end of the photo album with the current timestamp instead of the timestamp that I'm trying to set.
It appears the Photos app does not sort based on the date in the image's metadata (most likely because that metadata can be stored on iCloud, it's potentially not available locally). Instead it appears to sort by the creationDate
, which is a property defined on PHAsset
. You can change that using the Photos framework. Something like this should do the trick:
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
let request = PHAssetChangeRequest(forAsset: asset)
request.creationDate = dateTime!
}, completionHandler: { (success: Bool, error: NSError?) -> Void in
dispatch_async(dispatch_get_main_queue()) {
//done
}
})
However, note that the Camera Roll or All Photos album does not sort them the same as the Years, Collections, and Moments view. For some reason Camera Roll/All Photos does not sort on the creationDate
while Years, Collections, and Moments does. It doesn't use the photo metadata either, so I am not sure what it is examining to sort the photos for that album.