Skip to content

Instantly share code, notes, and snippets.

@jeanetienne
Last active January 18, 2024 21:28
Show Gist options
  • Save jeanetienne/76ee42335f80c09d6dafc58169c669fe to your computer and use it in GitHub Desktop.
Save jeanetienne/76ee42335f80c09d6dafc58169c669fe to your computer and use it in GitHub Desktop.
Drawing an NSImage with rounded corners in Swift
// This extension is a port of @venj's solution from 2011
// https://github.com/venj/Cocoa-blog-code/blob/master/Round%20Corner%20Image/Round%20Corner%20Image/NSImage%2BRoundCorner.m
extension NSImage {
func roundCorners(withRadius radius: CGFloat) -> NSImage {
let rect = NSRect(origin: NSPoint.zero, size: size)
if
let cgImage = self.cgImage,
let context = CGContext(data: nil,
width: Int(size.width),
height: Int(size.height),
bitsPerComponent: 8,
bytesPerRow: 4 * Int(size.width),
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue) {
context.beginPath()
context.addPath(CGPath(roundedRect: rect, cornerWidth: radius, cornerHeight: radius, transform: nil))
context.closePath()
context.clip()
context.draw(cgImage, in: rect)
if let composedImage = context.makeImage() {
return NSImage(cgImage: composedImage, size: size)
}
}
return self
}
}
fileprivate extension NSImage {
var cgImage: CGImage? {
var rect = CGRect.init(origin: .zero, size: self.size)
return self.cgImage(forProposedRect: &rect, context: nil, hints: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment