用swift导致CGBitmapContextCreate错误

时间:2014-06-08 18:00:41

标签: swift cgcontext

我正在尝试在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 个答案:

答案 0 :(得分: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)

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

答案 1 :(得分: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
    }

答案 2 :(得分: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);

全部'rawValue'正在进行中:)

你甚至不需要将bitmapInfo分开,并且可以做一行:

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

答案 3 :(得分:3)

我在使用UInt的Swift 1.2中遇到了一些问题,现在我正在使用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 :(得分:2)

建议的方式兼容 Xcode 8.3 Xcode 9 ,支持 Swift 3 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
    }

答案 5 :(得分: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)

答案 6 :(得分:1)

已针对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具有高度和宽度

答案 7 :(得分:0)

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

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