2014-10-29 76 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

相關問題