Screenshot images from Gallery has bitsPerComponent = 16 (iOS)

Hello) I use PHImageManager.default() for fetch UIImage from iOS gallery (Photo App). I noticed that screenshot images from galley has bitsPerComponent value equal 16. That's supported only in Mac OS. I could ONLY fix this by redrawing the image using UIGraphicsBeginImageContextWithOptions.

    private func redrawImageTo8Bit() -> UIImage? {
        // UIGraphicsImageRenderer can not change bitsPerComponent value
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
        draw(in: CGRect(origin: .zero, size: size))
        let redrawedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return redrawedImage
    }

https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html

Example of using PHImageManager.default

PHImageManager.default().requestImage(
            for: phAsset,
            targetSize: size,
            contentMode: contentMode,
            options: options
        ) { [weak self] image, info in
            if let image = image // Screenshot of an image taken on the device (iPhone 12 Pro Max) {
                 let bitsPerComponent = image.cgImage?.bitsPerComponent // 16
            }
        }

There should be no need to process the image – the chart your referring to only referenced what formats were supported by CGBitmapContextCreate(), and it is also somewhat out of date (many of the Mac OS X only formats it references were added a few years ago).

CGImages support many more formats than are possible to generate with a bitmap context, so for displaying the image you generally don't need to worry about its internal format.

Screenshot images from Gallery has bitsPerComponent = 16 (iOS)
 
 
Q