Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active November 5, 2022 23:02
Show Gist options
  • Save mminer/fa332673230aca6dc6278f8b5b7d6beb to your computer and use it in GitHub Desktop.
Save mminer/fa332673230aca6dc6278f8b5b7d6beb to your computer and use it in GitHub Desktop.
Creates a new NSImage with a circular mask.
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