Question

I need to be able to draw onto an image (borders, text annotations, etc) and then display it in a window as fast as possible.

Which Mac OS technology would be best for this purpose? Between the various bitmap contexts, CALayer, CGLayer, Quartz, OpenGL, CoreGraphics etc. it's really hard to know where to start.

I have working code that takes an NSImage, turns it into a CGImageRef to be worked on, then turns it back into an NSImage so that it can be display in an NSImageWell, however it's really not quick, even with very basic manipulation. I suspect the conversion to a CGImage and back isn't helping.

Any insights into the best way to handle this using the latest versions of Mac OS?

Was it helpful?

Solution 2

Do you really need to modify the image itself or can you just make a custom NSView that draws the unmodified image and then puts the annotations over it? That’d be super-fast.

OTHER TIPS

I’d start simple and see if it’s fast enough, by creating an NSImage of the correct size and locking focus on it and drawing directly into it. e.g.:

[myImage lockFocus]; {
    // draw annotations
    [[NSColor redColor] set];
    NSRectFill(someRect);
} [myImage unlockFocus];

Apple has spent some time optimizing NSImage in the last couple releases so it’s worth a shot since it’s so easy.


If that fails directly blatting buffers using OpenGL or even SceneKit would be faster than heck, but involve 60-100 lines of code.

I actually don’t know how to do it in OpenGL off the top of my head, but there are tons of tutorials on sending a basic texture to a window, including this one.

SceneKit would be equally fast and involve a lot less code, but with either of these approaches you still need to worry about creating an image buffer that that OpenGL accepts (relatively easy) and that you can edit using standard CGContext APIs (more code).

If you take this approach, look at CGBitmapContextCreateWithData() and its friends.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top