Last active
November 5, 2022 23:02
-
-
Save mminer/fa332673230aca6dc6278f8b5b7d6beb to your computer and use it in GitHub Desktop.
Creates a new NSImage with a circular mask.
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 AppKit | |
extension NSImage { | |
/// Copies this image to a new one with a circular mask. | |
func oval() -> NSImage { | |
let image = NSImage(size: size) | |
image.lockFocus() | |
NSGraphicsContext.current?.imageInterpolation = .high | |
let frame = NSRect(origin: .zero, size: size) | |
NSBezierPath(ovalIn: frame).addClip() | |
draw(at: .zero, from: frame, operation: .sourceOver, fraction: 1) | |
image.unlockFocus() | |
return image | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment