Combining CGBitmapInfo and CGImageAlphaInfo in Swift

You can make it a little simpler:

let bimapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
    .union(.ByteOrder32Little)

You unfortunately can't get away from converting the CGImageAlphaInfo into a CGBitmapInfo. That's just a weakness in the current API. But once you have it, you can use .union to combine it with other values. And once the enum type is known, you don't have to keep repeating it.

It's weird to me that there's no operator available here. I've opened a radar for that, and included an | implementation. http://www.openradar.me/23516367

public func |<T: SetAlgebraType>(lhs: T, rhs: T) -> T {
    return lhs.union(rhs)
}

@warn_unused_result
public func |=<T : SetAlgebraType>(inout lhs: T, rhs: T) {
    lhs.unionInPlace(rhs)
}

let bimapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
    | .ByteOrder32Little

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.