I'd like to convert the UIImage to a 24-bit RGB with no inbetween 32-bit RGBA buffer.
I tried this, but it does not work (bytes is all zero) :
public func pixelsRGB() -> [UInt8]? {
let size = self.size
let dataSize = size.width * size.height * 3
var pixelData = [UInt8](repeating: 0, count: Int(dataSize))
let colorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGContext(data: &pixelData,
width: Int(size.width),
height: Int(size.height),
bitsPerComponent: 8,
bytesPerRow: 3 * Int(size.width),
space: colorSpace,
bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)
guard let cgImage = self.cgImage else {
return nil
}
context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
return pixelData
}