Created
April 21, 2020 12:14
-
-
Save iSapozhnik/1aa7a0114e0dc49496b51dff5e679362 to your computer and use it in GitHub Desktop.
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
extension NSImage { | |
func appIcon(h:CGFloat = 35) -> NSImage { | |
let size = NSSize(width: h, height: h) | |
let cornerRadius: CGFloat = h/5 | |
guard self.isValid else { | |
return self | |
} | |
let newImage = NSImage(size: size) | |
self.size = size | |
newImage.lockFocus() | |
NSGraphicsContext.current?.imageInterpolation = .high | |
NSGraphicsContext.saveGraphicsState() | |
let path = NSBezierPath(roundedRect: NSRect(origin: NSPoint.zero, size: size), xRadius: cornerRadius, yRadius: cornerRadius) | |
path.addClip() | |
self.draw(at: NSPoint.zero, from: NSRect(origin: NSPoint.zero, size: size), operation: .copy, fraction: 1.0) | |
NSGraphicsContext.restoreGraphicsState() | |
newImage.unlockFocus() | |
return newImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment