Skip to content

Instantly share code, notes, and snippets.

@rishigesh
Created March 17, 2017 15:26
Show Gist options
  • Save rishigesh/e09bc943ef1e83852c2ac21c3e1b1f35 to your computer and use it in GitHub Desktop.
Save rishigesh/e09bc943ef1e83852c2ac21c3e1b1f35 to your computer and use it in GitHub Desktop.
NSImage Mask image with NSColor (Swift)
func mask(color: NSColor) -> NSImage {
let imageRect = CGRect(x: CGFloat(0.0), y: CGFloat(0.0), width: CGFloat(self.size.width), height: CGFloat(self.size.height))
var newImage:NSImage? = nil
let context = IOSXGraphicsBeginImageContextWithOptions(imageRect.size, false, 1.0)
context.clip(to: imageRect, mask: self.cgImage!)
context.setFillColor(color.cgColor)
context.setBlendMode(.difference)
context.fill(imageRect)
newImage = IOSXGraphicsGetImageFromImageContext(context)
return newImage!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment