Exporting images with metadata to the photo gallery in Swift 3/iOS 10+

With iOS10 and Swift 3, Apple removed ALAsset and replaced it with the Photos library.

While the concepts of the new Photos library are great, the documentation is not very clear on how to write custom metadata to the image. The fact that UIImage strips most metadata does not make this issue any easier.

After a while of digging around in the iOS SDK Docs, I found the following to work well:

Basically, write the image using the ImageIO library, which supports custom metadata. Then, add the image to the photo gallery via URL, using the Photos library.

Once you figure it out, it’s simple!