ios 我在 swift 中收到不支持的参数组合 CGBitmap 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24109149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I am getting unsupported parameter combination CGBitmap error with swift
提问by loopmasta
I am trying to create a CGContext in swift. It compiles but throws an error at runtime.
我正在尝试快速创建一个 CGContext 。它编译但在运行时抛出错误。
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let context:CGContext = CGBitmapContextCreate(nil, 20, 20, 8, 0, colorSpace, CGBitmapInfo.AlphaInfoMask)
CGColorSpaceRelease(colorSpace);
....
And the error is:
错误是:
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
回答by loopmasta
Just in case somebody is running into the same problem. The snippet below finally works.
以防万一有人遇到同样的问题。下面的片段终于起作用了。
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)
It generates a 32 bit RGBA context in swift
它迅速生成 32 位 RGBA 上下文
回答by Echelon
Updated for Swift 3:
为 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
}
回答by Graham Perks
In Swift 2.1 one can access the fields properly, and even OR them together:
在 Swift 2.1 中,可以正确访问字段,甚至可以将它们组合在一起:
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)
let context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, bitmapInfo.rawValue);
Whole lot of 'rawValue' going on :)
大量的“rawValue”正在进行:)
You don't even need to separate out the bitmapInfo, and can do a one-liner:
你甚至不需要把bitmapInfo分开,就可以做一个单行:
let context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue
回答by abdullahselek
Suggested way compatible with both Xcode 8.3and Xcode 9which supports Swift 3and Swift 4
与支持Swift 3和Swift 4 的Xcode 8.3和Xcode 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
}
回答by jawadAli
Updated for Swift 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)
where rect has height and width
其中 rect 具有高度和宽度
回答by Miralem Cebic
I had some issues in Swift 1.2 using UInt
, now I'm using Int
and it's working.
This Example shows how to convert an Image to a grayscale image.
我在使用 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)
回答by Sam Soffes
In Swift 2.2:
在 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)
回答by Goz
CGBitmapInfo.AlphaInfoMask is not a valid bitmap info.
CGBitmapInfo.AlphaInfoMask 不是有效的位图信息。
Try setting CGBitmapInfo.AlphaLast or CGBitmapInfo.AlphaFirst.
尝试设置 CGBitmapInfo.AlphaLast 或 CGBitmapInfo.AlphaFirst。