I have been working on a macOS application recently where I encountered an interesting task to grey out flags for countries that are disabled.
I first tried overlaying a semi-transparent gray view (fun fact, you cannot directly set a background for a view in Cocoa, you need to use the layer) over the flag image but it did not look very good.
Luckily there is a way to convert a
NSImage to grayscale directly in Cocoa.
You first need to create a bitmap representation your
let bitmap = NSBitmapImageRep(cgImage: cgImage)
convert it to grayscale
let grayscale = bitmap.converting(to: .genericGray, renderingIntent: .default)
and then construct an
NSImage from the result.
let grayImage = NSImage(size: grayscale.size) greyImage.addRepresentation(grayscale)
Putting it all together as an NSImage extension might look like this[Read More]