Preserve your images' transparency when saving and sharing them, in Swift

Loosing your rounded corners or transparent background when saving an image to the user's photo library or sharing it with UIActivityViewController? It's because it's be turned into a JPEG. Here's how to solve that.

Preserve your images' transparency when saving and sharing them, in Swift
Photo by Bud Helisson / Unsplash

Let's say we are making an app. A chat app, a social media where the main medium are pictures, an image generation app using the latest GAN models machine learning has to offer, whatever. It's an iOS app, written in Swift, it shows images, and users can save those locally and re-share them through other apps, without losing the alpha (the transparency).