First, think of a UIImage
instance as a decoded generic bitmap image without any particular format to it. The UIImage
can be loaded from all sorts of data formats, including JPEG or PNG or etc, but once it's loaded up, it doesn't have a format or type to it.
Your image data appears to be provided to you in the form of a base64-encoded string. That string, which becomes binary data within the key
Data
object, does have some inherent image format to it. Yours may be a PNG or it may be a JPG-- I can't tell from the info in the question, and it doesn't necessarily matter. The idea is that UIImage
does you a convenience by loading your key
image data in any format. And it is also able to produce an encoded object of image data in the format you request later, if you wish. The share activity doesn't let you do that though-- it will choose the format for you, based on some internal rules (maybe resulting size, quality, features like transparency, etc). Sounds like for you, it's picking a JPG.
If you want to share the image, but you always want to only provide it as a PNG, then you'll probably have ask for the encoding yourself and share it as a file (with a type and filename).
I.e. if you need to encode the image to a PNG, you will indeed need to instantiate the UIImage
, and then ask it for .pngData()
, and use that Data
. However, if you know for sure that your original data (key
) was already in PNG format and you just want to use that, then you don't need to use the UIImage
to round-trip the data, you can just use your original key
as your PNG file.
If you go the route of providing your file directly, then you can either save that data to disk (/tmp
seems right) and provide the URL to the activity view controller as the activityItems
, or you can probably do it directly with the in-memory Data
, but this may require you to create a small container class around the data object that implements the UIActivityItemSource
protocol in order to be able to provide information about e.g. the type to the share sheet.
A good question for you of course is: why do you only want a PNG file here? Is there a reason why just using the default share activity behavior doesn't work for you?