Last active
April 9, 2025 12:23
-
-
Save jeanetienne/76ee42335f80c09d6dafc58169c669fe to your computer and use it in GitHub Desktop.
Drawing an NSImage with rounded corners in Swift
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
// 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