我正在尝试在Swift中创建一个CGContext。代码编译通过,但在运行时抛出了错误。
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let context:CGContext = CGBitmapContextCreate(nil, 20, 20, 8, 0, colorSpace, CGBitmapInfo.AlphaInfoMask)
CGColorSpaceRelease(colorSpace);
....
错误信息如下:
Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; unrecognized; 96 bytes/row.
fatal error: Can't unwrap Optional.None
UInt32
而不是一个CGBitMapInfo
对象,因此在Swift 2.0中使用let context = CGBitmapContextCreate(nil, Int(rect.size.width), Int(rect.size.height), 8, 0, colorSpace, bitmapInfo.rawValue)
。请注意,我们通过使用其rawValue
将CGBitMapInfo
转换为UInt32
。 - the_critic