2014-10-29 156 views
8

我收到'kCGImageAlphaPremultipliedLast'的未解决标识符错误。 Swift找不到它。这在Swift中可用吗?Swift OpenGL未解析标识符kCGImageAlphaPremultipliedLast

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

回答

21

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

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

    static var AlphaInfoMask: CGBitmapInfo { get } 
    static var FloatComponents: CGBitmapInfo { get } 
    // ... 
} 

其中可能的 “阿尔法信息” 位作为一个枚举单独定义:

enum CGImageAlphaInfo : UInt32 { 
    case None /* For example, RGB. */ 
    case PremultipliedLast /* For example, premultiplied RGBA */ 
    case PremultipliedFirst /* For example, premultiplied ARGB */ 
    // ... 
} 

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

let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue) 
let gc = CGBitmapContextCreate(..., bitmapInfo) 

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

public struct CGBitmapInfo : OptionSetType 

,它可以与

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue) 
+1

感谢初始化你刚才救了我大量的时间! – NJGUY 2014-10-29 20:54:36

+0

@Martin R非常感谢! – BurtK 2016-03-27 13:34:01