Created
March 17, 2017 15:26
-
-
Save rishigesh/e09bc943ef1e83852c2ac21c3e1b1f35 to your computer and use it in GitHub Desktop.
NSImage Mask image with NSColor (Swift)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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