开发者

Porting Quartz 2d python demo to pure Core Graphics C

开发者 https://www.devze.com 2023-01-10 05:47 出处:网络
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 Q

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 = CGBitmapCo开发者_开发问答ntextCreateImage (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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号