iOS 17 Problems in getting RGB color from image

I have written and used the code to get the colors from CGImage and it worked fine up to iOS16. However, when I use the same code in iOS17, Red and Blue out of RGB are reversed.

Is this a temporary bug in the OS and will it be fixed in the future? Or has the specification changed and will it remain this way after iOS17?

Here is my code:

let pixelDataByteSize = 4
guard let cfData = image.cgImage?.dataProvider?.data else { return }
let pointer:UnsafePointer = CFDataGetBytePtr(cfData)
let scale = UIScreen.main.nativeScale
let address = ( Int(image.size.width * scale) * Int(image.size.height * scale / 2) + Int(image.size.width * scale / 2) ) * pixelDataByteSize
let r = CGFloat(pointer[address])   / 255
let g = CGFloat(pointer[address+1]) / 255
let b = CGFloat(pointer[address+2]) / 255
  • The order of the color channels in a CGImage depends on its alphaInfo and bitmapInfo. The default format for 8-bit images is actually BGRA (instead of RGBA) in many places. Where do you get the image from? What's its bitmapInfo and does it differ between iOS 16 and 17?

Add a Comment