将 Quartz 2d python 演示移植到纯 Core Graphics C

发布于 2024-09-13 06:57:00 字数 1553 浏览 4 评论 0原文

首先让我指出,我完全不知道我在用 Objective-C 和 mac 开发做什么(尽管我对 c 很满意)。我在 leopard 上使用 Python 的 Quartz-2d 绑定制作了一个非常简单的图形实用程序:

http:// developer.apple.com/graphicsimaging/pythonandquartz.html

基本上输入一个文本文件并写入一个漂亮的 png 文件(它是一个命令行实用程序)。我很高兴,直到我将该实用程序移至我们的雪豹服务器上,并发现雪豹上的 CoreGraphics 和 32 位 python 存在各种问题。其中一些问题可以解决,有些则不能。因此,我尝试将这个简单的实用程序脚本移植到 Objective-C(实际上是 CI 假设)并遇到了一些问题。有谁知道是否有一个很好的示例几乎与 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 是一个来自 Apple 关于quartz 2d 的指南

TL;DR 有谁有上面链接中给出的 python 演示的 C 端口吗?

let me first off noting that I have absolutely no idea what I'm doing with objective-c and mac development (though I'm fine with c). I made a wonderfully simple graphics utility on leopard with the Quartz-2d binding for python:

http://developer.apple.com/graphicsimaging/pythonandquartz.html

that basically inputs a text file and writes a nice png file (it's a command line utility). I was happy as a pig in mud until I moved the utility to our snow leopard servers and discovered that there were all sorts of issues with CoreGraphics and 32 bit python on snow leopard. Some of these issues are soluble, and some not. So, I'm attempting to port this simple utility script to objective-c (really C I suppose) and running into a few issues. Does anyone else know if there's a nice example almost exactly like the one given in python and quartz, but all in native code?

My major issue is writing the graphics context to a file

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 is a simple function from apple's guide on quartz 2d.

TL;DR Does anyone have a C port of the python demo given in the above link?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2024-09-20 06:57:00
CGImageRef myImage = CGBitmapContextCreateImage(myBitmapContext);// 5

CGContextDrawImage(myBitmapContext, myBoundingBox, myImage);// 6

什么?为什么要将上下文的内容捕获为图像,然后将该图像绘制回您从中获取的上下文中?

// 我想在这里写入一个文件!

执行第 5 步,然后将该图像提供给 CGImageDestination

您可能会发现核心图形参考集合很方便。大多数框架都有一个类似的文档。

CGImageRef myImage = CGBitmapContextCreateImage (myBitmapContext);// 5

CGContextDrawImage(myBitmapContext, myBoundingBox, myImage);// 6

What? Why would you capture the contents of the context as an image, and then draw that image back into the context you got it from?

// I'd like to write to a file here!

Do step 5, then feed that image to a CGImageDestination.

You may find the Core Graphics Reference Collection handy. There's a document like that for each of most of the frameworks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文