0

This is somewhat building on a question from a while back, but I am working on a solution to allow a user to choose an image from their Photos library, via UIImagePickerController, and if said image is a .png with transparency, to maintain the transparency of the image for display in a UIImageView.

I can consistently replicate my issue using the following steps;

  • Import my image into Photos on my Mac.
  • Allow my image to sync, via iCloud, to Photos on my iOS device.
  • Attempt the following code to load my image in my app;

-

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

       let png = UIImagePNGRepresentation(pickedImage)
       let pngImage = UIImage(data: png!)


       // TEST FILE TYPE
       if let fileURL = info[UIImagePickerControllerImageURL] as? URL { 
            // CONSISTENTLY PRINTS PUBLIC.JPEG   
            print(fileURL.typeIdentifier)    
        }
    }
}

For whatever reason, the process of importing the image into Photos is causing iOS to see the image as a JPEG, not a PNG, thus losing its transparency when tested in an UIImageView. The file name remains .png, but it is not feasible for use in the app.

As an aside, if I bring my transparent.png into my Xcode project, and set my UIImageView to imageView.image = UIImage(named: "newTest.png"), the transparency functions completely normally.

Update: This appears to be an issue that is inconsistently reproducible on iOS 11. Other users on SO and other message boards have confirmed the situation. At the moment, my suggested workaround is to discern a solution where a transparent PNG is downloaded from an external source, rather than being loaded from a user's Photos library. A restart of the iOS device does appear to resolve the issue for an undetermined amount of time.

ZbadhabitZ
  • 2,753
  • 1
  • 25
  • 45

1 Answers1

0

Make sure iCloud Photo Library's optimize setting is turned off:

On your iPhone, iPad, or iPod touch:

  1. Tap Settings > [your name] > iCloud. If you’re using iOS 10.2 or earlier, tap Settings > iCloud.
  2. Tap Photos.
  3. Choose Optimize [device] Storage.

On your Mac:

  1. Open the Photos app and click Photos in the menu bar.
  2. Click Preferences.
  3. Go to the iCloud tab and choose a storage setting.

If it is on, you will have to re-add the image to your photos library after it has been turned off for it to preserve the original.

Community
  • 1
  • 1
Jake G
  • 1,185
  • 9
  • 19
  • Thank you for your suggestion! This was a good tip but I unfortunately cannot consistently replicate the issue. In fact, I'm led to believe this is an iOS issue. After a restart of my device, the issue has disappeared; my transparent .png remains transparent in Photos, Camera Roll, etc. After a few hours, that same transparent .png becomes a .jpg, with no user interaction. I've filed a Radar but am working to determine if your suggestion could be a culprit. – ZbadhabitZ Mar 22 '18 at 16:26