KCGImageAlphaNone unresolved identifier in fast

I am trying to convert images / textures (for SpriteKit) to grayscale using CoreImage in Swift:

I found this answer: https://stackoverflow.com/a/4646262/232632 which is trying to convert to swift for iOS7 / 8

But kCGImageAlphaNonedoes not exist. I can use it instead CGImageAlphaInfo.None, but the function is CGBitmapContextCreate()not like as the last parameter. I must use enum CGBitmapInfo, but there is no equivalent inside kCGImageAlphaNone.

This is the full line with the wrong parameter (last):

var context = CGBitmapContextCreate(nil, UInt(width), UInt(height), 8, 0, colorSpace, kCGImageAlphaNone)

I just need a way to convert UIImage to grayscale in Swift.

+4
source share
2 answers

You need to create struct CGBitmapInfofrom the value CGImageAlphaInfo.None:

let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.None.rawValue)
var context = CGBitmapContextCreate(nil, UInt(width), UInt(height), 8, 0, colorSpace, bitmapInfo)
+11
source

I believe the source code for Objective-C was wrong. The value of an enumeration was kCGImageAlphaNonenot a suitable type for passing, but Objective-C is less secure (an enumerated enumeration is an enumeration), so it worked. The raw value kCGImageAlphaNoneis equal 0, so passing CGBitmapInfo.allZerosor CGBitmapInfo.ByteOrderDefault(both of which have the original values 0) should give you the same results.

+1
source

All Articles