UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

3      

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.

3      

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.

Steve

3      

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.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.