苹果的一些插件API,如Webkit插件、iTunes可视化插件(SDK)等,提供的图形上下文指针都是CGrafPtr这种类型。那么如果想用Cocoa在其中画图,可以参考如下代码:

  //destPort CGrafPtr

CGContextRef cgContext;

OSStatus err = QDBeginCGContext(destPort, &cgContext);

if(err == noErr)

{

[NSGraphicsContext saveGraphicsState];


[NSGraphicsContext setCurrentContext: [NSGraphicsContext  

  graphicsContextWithGraphicsPort: cgContext flipped: NO];

 

/* 在这里就可以画了 */

 

[NSGraphicsContext restoreGraphicsState];

 

QDEndCGContext(destPort, &cgContext);

}