Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining CGBitmapInfo and CGImageAlphaInfo in Swift

I'm rewriting Apple's UIImageEffects sample code from Objective-C to Swift and I have a question regarding the following line:

vImage_CGImageFormat format = {
    .bitsPerComponent = 8,
    .bitsPerPixel = 32,
    .colorSpace = NULL,
    .bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little,
    .version = 0,
    .decode = NULL,
    .renderingIntent = kCGRenderingIntentDefault
};

Here is my version in Swift:

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue)
let format = vImage_CGImageFormat(bitsPerComponent: 8, bitsPerPixel: 32, colorSpace: nil, bitmapInfo: bitmapInfo, version: 0, decode: nil, renderingIntent: .RenderingIntentDefault)

Is this the simplest way to create bitmapInfo in Swift?

like image 459
Rudolf Adamkovič Avatar asked Nov 12 '15 14:11

Rudolf Adamkovič


1 Answers

It’s not pretty right now no matter what you do, but I think the cleanest style (as of Swift 4) is to use something like:

let bitmapInfo: CGBitmapInfo = [
      .byteOrder32Little,
      .floatComponents,
      CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)]

(Or use something similar inline.) This at least preserves the essential optionset-iness of the info.

like image 157
Wil Shipley Avatar answered Nov 06 '22 09:11

Wil Shipley