我使用Swift时遇到了不支持的参数组合CGBitmap错误。

34
我正在尝试在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
8个回答

63

如果有人遇到相同的问题,以下代码片段最终可行。

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)

它在Swift中生成一个32位的RGBA上下文


6
使用当前版本的Swift(我正在使用Xcode 6.1):让bitmapInfo等于CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue) - Ryan H.
4
目前的 Xcode 版本为: let context = CGBitmapContextCreate(nil, Int(rect.size.width), Int(rect.size.height), 8, 0, colorSpace, bitmapInfo)。 - Janusz Chudzynski
14
在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)。请注意,我们通过使用其rawValueCGBitMapInfo转换为UInt32 - the_critic
Swift 2 let context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, bytesPerRow, colourSpace, CGBitmapInfo.ByteOrder32Little.rawValue) Swift 2中,让context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, bytesPerRow, colourSpace, CGBitmapInfo.ByteOrder32Little.rawValue) - Khaled Annajar
我想使用PremultipliedLast以外的其他选项,但如果我选择了none、first或last,上下文将变为nil。 - Iraniya Naynesh

18

已更新至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
    }

11
在 Swift 2.1 中,一个可以正确访问字段,甚至还可以将它们进行 OR 运算:

在Swift 2.1中,一个可以正确访问字段,甚至还可以将其们进行OR运算:

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

let context = CGBitmapContextCreate(baseAddress, width, height, 8,
                bytesPerRow, colorSpace, bitmapInfo.rawValue);

有很多'rawValue'在进行中 :)

你甚至不需要分离出位图信息,可以用一行代码实现:

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

6

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)

rect具有高度和宽度


CGImageAlphaInfo.premultipliedLast.rawValue。真的吗,苹果? - Maury Markowitz

4

我在使用Swift 1.2时遇到了一些使用UInt的问题,现在我改用Int并且它正常工作了。这个示例展示了如何将图像转换为灰度图像。

let imageRect = self.myImage.frame

let colorSpace = CGColorSpaceCreateDeviceGray()
let width = imageRect.width
let height = imageRect.height

let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.None.rawValue)
let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo)

4

建议的方法适用于同时支持 Xcode 8.3Xcode 9,并支持 Swift 3Swift 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
    }

1
在Swift 2.2中:

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue).rawValue
let colorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo)

0

CGBitmapInfo.AlphaInfoMask不是有效的位图信息。

尝试设置CGBitmapInfo.AlphaLast或CGBitmapInfo.AlphaFirst。


这不编译,我在https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CGBitmapContext/index.html#//apple_ref/c/func/CGBitmapContextCreate上找不到CGBitmapInfo.AlphaLast或CGBitmapInfo.AlphaFirst。 - loopmasta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接