Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift OpenGL unresolved identifier kCGImageAlphaPremultipliedLast

I am getting a unresolved identifier error for 'kCGImageAlphaPremultipliedLast'. Swift can't find it. Is this available in Swift?

var gc = CGBitmapContextCreate(&pixelData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, imageCS, bitmapInfo: kCGImageAlphaPremultipliedLast);
like image 426
NJGUY Avatar asked Oct 29 '14 20:10

NJGUY


1 Answers

The last parameter of CGBitmapContextCreate() is defined as a struct

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

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

where the possible "alpha info" bits are defined separately as an enumeration:

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

Therefore you have have to convert the enum to its underlying UInt32 value and then create a CGBitmapInfo from it:

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

Update for Swift 2: The CGBitmapInfo definition changed to

public struct CGBitmapInfo : OptionSetType

and it can be initialized with

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
like image 170
Martin R Avatar answered Oct 19 '22 16:10

Martin R