I am getting unsupported parameter combination CGBitmap error with swift

前端 未结 8 558
野趣味
野趣味 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条回答
  •  独厮守ぢ
    2020-12-09 08:26

    Suggested way compatible with both Xcode 8.3 and Xcode 9 which supports Swift 3 and Swift 4

    let colorSpace = CGColorSpaceCreateDeviceRGB()
    guard let bitmapContext = CGContext(data: nil, 
                                        width: Int(size.width),
                                        height: Int(size.height),
                                        bitsPerComponent: Int(bitsPerComponent),
                                        bytesPerRow: Int(bytesPerRow),
                                        space: colorSpace,
                                        bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
                                      return nil
        }
    

提交回复
热议问题