Skip to content

Instantly share code, notes, and snippets.

@iSapozhnik
Created April 21, 2020 12:14
Show Gist options
  • Save iSapozhnik/1aa7a0114e0dc49496b51dff5e679362 to your computer and use it in GitHub Desktop.
Save iSapozhnik/1aa7a0114e0dc49496b51dff5e679362 to your computer and use it in GitHub Desktop.
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