1

I have a function to import images from an array of NSItemProvider (passed from a PHPickerViewController) and I need to maintain transparency in the images. Any images in PNG format are correctly maintaining transparency but WebP images are not. The image import code is as follows:

private func save(itemProviders: [NSItemProvider]) async throws -> Int {
    return try await withCheckedThrowingContinuation({ continuation in
        let group = DispatchGroup()
        var failureCount = 0
        for item in itemProviders {
            // webp format image
            if item.hasItemConformingToTypeIdentifier(UTType.webP.identifier) {
                group.enter()
                item.loadDataRepresentation(forTypeIdentifier: UTType.webP.identifier) { data, error in
                    defer {
                        group.leave()
                    }
                    guard let data, let image = UIImage(data: data) else {
                        return
                    }
                    self.saveImage(image)
                }
            }
            else if item.canLoadObject(ofClass: UIImage.self) {
                // other image formats, including PNG
                group.enter()
                item.loadObject(ofClass: UIImage.self) { (image, error) in
                    defer {
                        group.leave()
                    }
                    guard let image = image as? UIImage else {
                        return
                    }

                    self.saveImage(image)
                }
            }
            else {
                failureCount += 1
            }
        }
        group.notify(queue: .main) {
            continuation.resume(returning: failureCount)
        }
    })
}

Please note that the saveImage function is a simple resize and save to disk function, and I have been able to verify that transparency is not being lost during that step.

It seems as though transparency is being lost during the call to item.loadDataRepresentation(forTypeIdentifier: UTType.webP.identifier) or when the resulting Data is used to instantiate a UIImage. The code in the WebP handling block seems to be irreducibly simple though so I'm not sure how I can possibly fix this issue.

stackunderflow
  • 754
  • 7
  • 18
  • Silly question but are you sure the WebP image you are selecting from the photo library has any transparency? Maybe the transparency was lost in whatever process put it into your photo library. – HangarRash Dec 24 '22 at 03:13
  • Not a silly question! The image has transparency in the photo album, because the background behind it is white in the library collection view, and it changes to a black (dark mode) background when I select the image and view it enlarged. That said, the PNG images I have show the correct dark mode background in both the library collection view and while enlarged. The difference in display between WebP and PNG in the library is strange and does hint at an issue along the lines of what you're alluding to, but the image background changing from white to black indicates that there is transparency. – stackunderflow Dec 24 '22 at 05:39

0 Answers0