TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Drawing graphics in MacOS

Forums > macOS

I need to draw some shapes into an NSImage and don't have a clue where to start. Almost everything I read online about CoreGraphics relates to iOS and UIGraphicsImageRenderer, which doesn't seem to apply in MacOS. I believe, if I can get the graphics context, I will be able to cobble together the drawing commands (I did somethinng recently to create PDF files using the PDF context in iOS), but I can't seem to find the link between an NSImage and getting it's drawing context.

Can someone point me at a tutorial or some sample code that does something that gets me started please?

I believe there is something called NSGraphicsContext, but don't know how to make the link between that and the NSImage.

I'm creating a status bar app with some developer tools and one of the tools I want to create will generate dummy images for me of a specified size and colour. I've coded everything except that minor irritation of actually drawing an image that I can them copy to the pasteboard or save to a file. The eventual aim is to have a "not frequently used" app that contains the things I do rarely so have to keep re-learning. It currently has a Lorem Ipsum generator and a color wheel. The image generator is the next tool.

Thank you.


A search for core graphics tutorial mac brought up the following Kodeco tutorial:

Core Graphics on macOS Tutorial

Kodeco also has a bunch of Core Graphics tutorials focused on iOS. Anything with a UI prefix on iOS usually has a Mac equivalent with a NS prefix.

The NSBezierPath class lets you create shapes and paths.

Apple also has an old Quartz 2D Programming Guide that you may find helpful.

You will have to convert the drawings to a Data object to create a NSImage.


Thanks for these. I started taking stuff from the Kodeco tutorial, but had a bunch of errors from XCode, so didnt get very far. I tend to be a bit impatient with tutorials. The link to the Quartz documentation is very useful though, thank you.

I also came across a book yesterday called "Graphics and Animations with SwiftUI for Masterminds" which appears to offer a number of SwiftUI specific solutions (Canvas and ImageRenderer). I'll give those a try too.

It's a bit sad that there are so many books and tutorials for iOS and very little up to date for MacOS.

Thank you very much.



In case anyone else ends up here, I got to a drawing context using this code...

    private func createDummyImage(size: CGSize) -> CGImage {

        let pixelsWide = Int(size.width)
        let pixelsHigh = Int(size.height)
        let bitmapBytesPerRow = pixelsWide * 4

        let bufferLength = Int(size.width * size.height * 4)

        let bitmapData: CFMutableData = CFDataCreateMutable(nil, 0)
        CFDataSetLength(bitmapData, CFIndex(bufferLength))
        let bitmap = CFDataGetMutableBytePtr(bitmapData)

        let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
        let context = CGContext(data: bitmap,
                                width: pixelsWide,
                                height: pixelsHigh,
                                bitsPerComponent: 8,
                                bytesPerRow: bitmapBytesPerRow,
                                space: colorSpace,
                                  bitmapInfo: bitmapInfo.rawValue)

Once I drew the content, I got a CGImage using:

        let image = context.makeImage()

Once I had that, I could convert to an NSImage for display on the screen. Loads of error handling to be added at some stage. For now, I'm only prototyping the code, so want the crashes to see what can go wrong.


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.