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
}
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
}
}