用swift导致CGBitmapContextCreate错误

loo*_*sta 29 cgcontext swift

我试图在swift中创建一个CGContext.它编译但在运行时抛出错误.

let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let context:CGContext = CGBitmapContextCreate(nil, 20, 20, 8, 0, colorSpace, CGBitmapInfo.AlphaInfoMask)
CGColorSpaceRelease(colorSpace);

....
Run Code Online (Sandbox Code Playgroud)

错误是:

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
Run Code Online (Sandbox Code Playgroud)

loo*_*sta 60

以防万一有人遇到同样的问题.下面的片段最终有效.

let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(nil, UInt(rect.size.width), UInt(rect.size.height), 8, 0, colorSpace, bitmapInfo)
Run Code Online (Sandbox Code Playgroud)

它在swift中生成32位RGBA上下文

  • Swift 2.0实际上需要一个`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` (14认同)
  • 使用当前版本的Swift(我正在使用Xcode 6.1):让bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue) (6认同)
  • 现在版本的Xcode现在是:let context = CGBitmapContextCreate(nil,Int(rect.size.width),Int(rect.size.height),8,0,colorSpace,bitmapInfo) (4认同)

Ech*_*lon 15

针对Swift 3进行了更新:

    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
    guard let context = CGContext.init(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: Int(bitsPerComponent), bytesPerRow: Int(bytesPerRow), space: colorSpace, bitmapInfo: UInt32(bitmapInfo.rawValue)) else {
        // cannot create context - handle error
    }
Run Code Online (Sandbox Code Playgroud)


Gra*_*rks 10

在Swift 2.1中,可以正确访问字段,甚至可以将它们组合在一起:

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)

let context = CGBitmapContextCreate(baseAddress, width, height, 8,
                bytesPerRow, colorSpace, bitmapInfo.rawValue);
Run Code Online (Sandbox Code Playgroud)

很多'rawValue'继续:)

你甚至不需要分离出bitmapInfo,并且可以做一个单行:

let context = CGBitmapContextCreate(baseAddress, width, height, 8,
                bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue
Run Code Online (Sandbox Code Playgroud)


use*_*632 5

Swift 5更新:

 let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
 let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
 let context = CGContext(data: nil, width: UInt(rect.size.width), height: UInt(rect.size.height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
Run Code Online (Sandbox Code Playgroud)

其中 rect 具有高度和宽度