Search code examples
cocoacore-animationcore-graphicsbeziernsimage

NSBezierPath to NSImage in order to avoid CoreAnimation


I have an app that currently has this line:

[myView setWantsLayer:YES];

In order to draw a GUI element via NSBezierPath. This line is required, otherwise when the user types in an adjacent (and overlapping) NSTextField, the contents of myView shudders.

I discovered that calling CoreAnimation loads the OpenGL framework, but does not unload it. See this question.

I think I can get around this by drawing the NSBezierPath to NSImage and then to display the NSImage in lieu of the NSBezierPath, but I haven't found a single source that shows me how to go about this.


Edit:

I should note that I want to save this BEFORE The NSBezierPath is displayed - so solutions that draw an existing view to an NSImage are not useful.


Question:

Can someone point me in the right direction for converting NSBezierPath to an NSImage?


Solution

  • You can draw anything directly into an NSImage, and you can create a blank NSImage. So, create an image whose size is the size of the bounds of the path, translate the path so that it's at the origin of the image, and then lock focus on the image, draw the path, and unlock focus.