34

我正在尝试快速创建一个 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
4

8 回答 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 上下文

于 2014-06-09T09:59:25.860 回答
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
    }
于 2016-10-27T10:09:27.733 回答
11

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

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
于 2015-09-23T19:50:22.600 回答
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)

其中 rect 有高度和宽度

于 2019-12-11T07:50:40.150 回答
4

我在 Swift 1.2 using 中遇到了一些问题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)
于 2015-04-28T11:20:28.500 回答
4

与支持Swift 3Swift 4的Xcode 8.3Xcode 9兼容的建议方式

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
    }
于 2017-04-03T15:40:12.333 回答
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)
于 2016-04-29T21:05:57.757 回答
0

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

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

于 2014-06-08T19:18:43.550 回答