Search code examples
macoscgcontextnsimage

How to DrawImage on OSX with CGContext


I have a CGContext and I tried to draw an image on it. I have a NSImage * load that i used on other method .

NSImage *check = [[NSImage alloc] initWithContentsOfFile:@"check.icns"];
CGContextDrawImage(arg2, CGRectMake(0, 0, 145, 15), check);

But CGContext want a CGImage * how can I used my NSImage * ?

Thanks


Solution

  • You should consider just opening it as a CGImage in this case, if that is all you will need it for. If an NSImage is what you need, then see -[NSImage CGImageForProposedRect:context:hints:]. This method may not require a copy, and it can produce a CGImage representation ideal for drawing into the destination context.

    If needed, you can create a NSGraphicsContext from a CGContext using +[NSGraphicsContext graphicsContextWithGraphicsPort:flipped:].