Last active
December 6, 2024 19:17
-
-
Save usagimaru/c0a03ef86b5829fb9976b650ec2f1bf4 to your computer and use it in GitHub Desktop.
NSImage+TintColor
This file contains 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
import Cocoa | |
// This will work with Swift 5 | |
extension NSImage { | |
func image(with tintColor: NSColor) -> NSImage { | |
if self.isTemplate == false { | |
return self | |
} | |
let image = self.copy() as! NSImage | |
image.lockFocus() | |
tintColor.set() | |
let imageRect = NSRect(origin: .zero, size: image.size) | |
imageRect.fill(using: .sourceIn) | |
image.unlockFocus() | |
image.isTemplate = false | |
return image | |
} | |
} | |
/* Usage | |
let sourceImage = NSImage(named: NSImage.Name("ImageName"))! | |
sourceImage.isTemplate = true | |
let tintColor = NSColor(red: 1.0, green: 0.08, blue: 0.50, alpha: 1.0) | |
let coloredImage = sourceImage.image(with: tintColor) | |
imageView.image = coloredImage | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Cyberbeni @robinstewart
Thanks for some advices!