Tinting a grayscale NSImage (or CIImage)

后端 未结 8 1915
轮回少年
轮回少年 2021-01-30 09:02

I have a grayscale image which I want to use for drawing Cocoa controls. The image has various levels of gray. Where it is darkest, I want it to draw a specified tint color dark

8条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 09:43

    Swift version in form of Extension :

    extension NSImage {
        func tintedImageWithColor(color:NSColor) -> NSImage {
            let size        = self.size
            let imageBounds = NSMakeRect(0, 0, size.width, size.height)
            let copiedImage = self.copy() as! NSImage
    
            copiedImage.lockFocus()
            color.set()
            NSRectFillUsingOperation(imageBounds, .CompositeSourceIn)
            copiedImage.unlockFocus()
    
            return copiedImage
        }
    }
    

提交回复
热议问题