将Quartz 2D Python示例移植到纯Core Graphics C
首先,我得说我对Objective-C和Mac开发完全不懂(不过我对C语言还可以)。我在Leopard系统上用Python的Quartz-2D绑定做了一个非常简单的图形工具:
http://developer.apple.com/graphicsimaging/pythonandquartz.html
这个工具基本上是读取一个文本文件,然后生成一个漂亮的PNG文件(它是一个命令行工具)。我用得很开心,直到把这个工具移到我们的Snow Leopard服务器上,才发现CoreGraphics和32位Python在Snow Leopard上有各种问题。有些问题可以解决,有些则不行。所以,我现在尝试把这个简单的工具脚本移植到Objective-C(其实应该是C),但遇到了一些麻烦。有没有人知道有没有一个和Python和Quartz中给的例子几乎一模一样的,完全用原生代码写的例子?
我最大的问题是如何把图形上下文写入文件。
myBitmapContext = MyCreateBitmapContext (400, 300);
CGContextSetRGBFillColor (myBitmapContext, 1, 0, 0, 1);
CGContextFillRect (myBitmapContext, CGRectMake (0, 0, 200, 100 ));
CGContextSetRGBFillColor (myBitmapContext, 0, 0, 1, .5);
CGContextFillRect (myBitmapContext, CGRectMake (0, 0, 100, 200 ));
CGImageRef myImage = CGBitmapContextCreateImage (myBitmapContext);// 5
CGContextDrawImage(myBitmapContext, myBoundingBox, myImage);// 6
char *bitmapData = CGBitmapContextGetData(myBitmapContext); // 7
// I'd like to write to a file here!
CGContextRelease (myBitmapContext);// 8
if (bitmapData) free(bitmapData); // 9
CGImageRelease(myImage);
MyCreateBitmapContext是来自于苹果的Quartz 2D指南的一个简单函数。
总结一下:有没有人有上面链接中Python示例的C语言版本?
1 个回答
1
CGImageRef myImage = CGBitmapContextCreateImage (myBitmapContext);// 5 CGContextDrawImage(myBitmapContext, myBoundingBox, myImage);// 6
什么?为什么要把上下文的内容捕捉成一张图片,然后再把这张图片画回到你获取它的上下文中呢?
// I'd like to write to a file here!
先完成第5步,然后把那张图片传给一个CGImageDestination。
你可能会觉得Core Graphics参考文档很有用。大多数框架都有类似的文档。