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.