Wrong colors in bitmap drawing (Creating image by context)

Viewed 71

Color distortion occurs when pictures are superimposed on each other. What am I doing wrong?

Alpha UIImage layer

Other UIImage layer

Alpha UIImage over other UIImage

// My pixel struct

struct RGBA: Codable {
        
   var r: UInt8
   var g: UInt8
   var b: UInt8
   var a: UInt8 
}

// Creating UIImage by pixels

let rgba = pixels.flatMap({ $0 })
let colorSpace = CGColorSpaceCreateDeviceRGB()
let data = UnsafeMutableRawPointer(mutating: rgba)

let bitmapContext = CGContext(data: data,
                              width: size.width,
                              height: size.height,
                              bitsPerComponent: 8,
                              bytesPerRow: 4 * size.width,
                              space: colorSpace,
                              bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)

guard let image = bitmapContext?.makeImage() else { return nil }
return UIImage(cgImage: image)

I tried using this code but it didn't work

CGBitmapInfo(rawValue: CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue).rawValue)

Thanks

0 Answers
Related