I am getting unsupported parameter combination CGBitmap error with swift

前端 未结 8 581
野趣味
野趣味 2020-12-09 08:16

I am trying to create a CGContext in swift. It compiles but throws an error at runtime.

let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let conte         


        
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 08:32

    In Swift 2.1 one can access the fields properly, and even OR them together:

    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)
    
    let context = CGBitmapContextCreate(baseAddress, width, height, 8,
                    bytesPerRow, colorSpace, bitmapInfo.rawValue);
    

    Whole lot of 'rawValue' going on :)

    You don't even need to separate out the bitmapInfo, and can do a one-liner:

    let context = CGBitmapContextCreate(baseAddress, width, height, 8,
                    bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue
    

提交回复
热议问题