0

I have these two lines of code that take Data and turns it into an UIImage, however the UIImage is a jpeg when I goto share it, how do I get it as a png and possibly set the file name?

let key = Data(base64Encoded: self.array[indexPath.row]["image"] as! String)!

let image = UIImage(data: key)

And this is how I am sharing it:

@IBAction func shareButtonPressed(_ sender: Any) {

        var imageToShare: Array<UIImage> = []

        for item in (collectionView?.indexPathsForSelectedItems)!
        {

            let cell = collectionView?.cellForItem(at: item) as! ImageCollectionCell

            imageToShare.append(cell.imageView.image!)

        }

        //Create Share Popup

        let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)

        //Assign source view

        activityViewController.popoverPresentationController?.sourceView = self.view

        //Present share popup

        self.present(activityViewController, animated: true, completion: nil)

    }
user979331
  • 11,039
  • 73
  • 223
  • 418
  • How are you sharing the image? – Sweeper Mar 18 '19 at 19:31
  • Possible duplicate of [iOS: sharing a transparent UIImage w/ UIActivityViewController?](https://stackoverflow.com/questions/24689926/ios-sharing-a-transparent-uiimage-w-uiactivityviewcontroller) – shim Mar 18 '19 at 19:45

2 Answers2

0

UIImage supports a variety of data types. The UIActivityViewController determines whether to share the image data as a PNG or JPEG (or other types), probably based on the underlying data in the image and/or the application/activity selected to share the data.

If you need it to be a PNG you can use pngData() on the image and share the Data object from that instead, but you can't necessarily guarantee that the app the user selects to share the image doesn't convert it to whatever it wants to use.

shim
  • 9,289
  • 12
  • 69
  • 108
0

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?

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204