【问题标题】:Swift OpenGL unresolved identifier kCGImageAlphaPremultipliedLastSwift OpenGL 未解析的标识符 kCGImageAlphaPremultipliedLast
【发布时间】:2015-10-10 18:19:19
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift opengl-es


    【解决方案1】:

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

    struct CGBitmapInfo : RawOptionSetType {
        init(_ rawValue: UInt32)
        init(rawValue: UInt32)
    
        static var AlphaInfoMask: CGBitmapInfo { get }
        static var FloatComponents: CGBitmapInfo { get }
        // ...
    }
    

    其中可能的“alpha 信息”位被单独定义为枚举:

    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)
    

    Swift 2 更新:CGBitmapInfo 定义更改为

    public struct CGBitmapInfo : OptionSetType
    

    它可以用初始化

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

    【讨论】:

    • 感谢您为我节省了大量时间!
    • @Martin R 非常感谢!
    【解决方案2】:
    猜你喜欢
    • 2016-04-29
    • 2014-08-10
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2016-11-28
    • 2015-05-25
    相关资源
    最近更新 更多