Swift OpenGL未解析的标识符kCGImageAlphaPremultipliedLast

NJG*_*GUY 10 opengl-es ios swift

我收到'kCGImageAlphaPremultipliedLast'的未解决的标识符错误.斯威夫特找不到它.这可以在Swift中使用吗?

var gc = CGBitmapContextCreate(&pixelData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, imageCS, bitmapInfo: kCGImageAlphaPremultipliedLast);
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 23

最后一个参数CGBitmapContextCreate()定义为结构

struct CGBitmapInfo : RawOptionSetType {
    init(_ rawValue: UInt32)
    init(rawValue: UInt32)

    static var AlphaInfoMask: CGBitmapInfo { get }
    static var FloatComponents: CGBitmapInfo { get }
    // ...
}
Run Code Online (Sandbox Code Playgroud)

其中可能的"alpha info"位分别定义为枚举:

enum CGImageAlphaInfo : UInt32 {
    case None /* For example, RGB. */
    case PremultipliedLast /* For example, premultiplied RGBA */
    case PremultipliedFirst /* For example, premultiplied ARGB */
    // ...
}
Run Code Online (Sandbox Code Playgroud)

因此,您必须将枚举转换为其基础UInt32值,然后CGBitmapInfo从中创建一个:

let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
let gc = CGBitmapContextCreate(..., bitmapInfo)
Run Code Online (Sandbox Code Playgroud)

更新斯威夫特2:CGBitmapInfo定义改为

public struct CGBitmapInfo : OptionSetType
Run Code Online (Sandbox Code Playgroud)

它可以用.初始化

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
Run Code Online (Sandbox Code Playgroud)