我们可以在Cocoa应用程序的drawRect:中获取一个Quartz Graphics Context,如下代码所示:
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
为了实现图2-1实例,需要先创建一个Cocoa应用程序。在Interface Builder中,拖动一个Custom View到窗口中,并子类化。然后实现子类视图的,如代码清单2-1所示。视图的drawRect:包含了所有的Quartz绘制代码。
注:NSView的drawRect:方法在每次视图需要绘制时自动调用。
Listing 2-1 Drawing to a window graphics context @implementation MyQuartzView - (id)initWithFrame:(NSRect)frameRect { self = [super initWithFrame:frameRect]; return self; } - (void)drawRect:(NSRect)rec { CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort]; //1 // ********** Your drawing code here ********** //2 CGContextSetRGBFillColor (myContext, 1, 0, 0, 1); //3 CGContextFillRect (myContext, CGRectMake (0, 0, 200, 100 )); //4 CGContextSetRGBFillColor (myContext, 0, 0, 1, .5); //5 CGContextFillRect (myContext, CGRectMake (0, 0, 100, 200)); //6 } @end
Quartz 2D API提供了两个函数来创建PDF Graphics Context:
Listing 2-2 Calling CGPDFContextCreateWithURL to create a PDF graphics context CGContextRef MyPDFContextCreate (const CGRect *inMediaBox, CFStringRef path) { CGContextRef myOutContext = NULL; CFURLRef url; url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, false); if (url != NULL) { myOutContext = CGPDFContextCreateWithURL (url, inMediaBox, NULL); CFRelease(url); } return myOutContext; }
Listing 2-3 Calling CGPDFContextCreate to create a PDF graphics context CGContextRef MyPDFContextCreate (const CGRect *inMediaBox, CFStringRef path) { CGContextRef myOutContext = NULL; CFURLRef url; CGDataConsumerRef dataConsumer; url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, false); if (url != NULL) { dataConsumer = CGDataConsumerCreateWithURL (url); if (dataConsumer != NULL) { myOutContext = CGPDFContextCreate (dataConsumer, inMediaBox, NULL); CGDataConsumerRelease (dataConsumer); } CFRelease(url); } return myOutContext; }
Listing 2-4 Drawing to a PDF graphics context CGRect mediaBox; mediaBox = CGRectMake (0, 0, myPageWidth, myPageHeight); myPDFContext = MyPDFContextCreate (&mediaBox, CFSTR("test.pdf")); CFStringRef myKeys[1]; CFTypeRef myValues[1]; myKeys[0] = kCGPDFContextMediaBox; myValues[0] = (CFTypeRef) CFDataCreate(NULL,(const UInt8 *)&mediaBox, sizeof (CGRect)); CFDictionaryRef pageDictionary = CFDictionaryCreate(NULL, (const void **) myKeys, (const void **) myValues, 1, &kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks); CGPDFContextBeginPage(myPDFContext, &pageDictionary); // ********** Your drawing code here ********** CGContextSetRGBFillColor (myPDFContext, 1, 0, 0, 1); CGContextFillRect (myPDFContext, CGRectMake (0, 0, 200, 100 )); CGContextSetRGBFillColor (myPDFContext, 0, 0, 1, .5); CGContextFillRect (myPDFContext, CGRectMake (0, 0, 100, 200 )); CGPDFContextEndPage(myPDFContext); CFRelease(pageDictionary); CFRelease(myValues[0]); CGContextRelease(myPDFContext);
注:位图Graphics Context有时用于后台绘制。CGLayer对象优化了后台绘制,因为Quartz在显卡上缓存了层。
iOS提示:iOS应用程序使用了UIGraphicsBeginImageContextWithOptions取代Quartz低层函数。如果使用Quartz创建一下后台bitmap,bitmap Graphics Context使用的坐标系统是Quartz默认的坐标系统。而使用UIGraphicsBeginImageContextWithOptions创建图形上下文,UIKit将会对坐标系统使用与UIView对象的图形上下文一样的转换。这允许应用程序使用相同的绘制代码而不需要担心坐标系统问题。虽然我们的应用程序可以手动调整CTM达到相同的效果,但这种做没有任何好处。
我们使用CGBitmapContextCreate来创建位图Graphics Context,该函数有如下参数:
Listing 2-5 Creating a bitmap graphics context CGContextRef MyCreateBitmapContext (int pixelsWide, int pixelsHigh) { CGContextRef context = NULL; CGColorSpaceRef colorSpace; void * bitmapData; int bitmapByteCount; int bitmapBytesPerRow; bitmapBytesPerRow = (pixelsWide * 4); bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); bitmapData = calloc( bitmapByteCount ); if (bitmapData == NULL) { fprintf (stderr, "Memory not allocated!"); return NULL; } context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); if (context== NULL) { free (bitmapData); fprintf (stderr, "Context not created!"); return NULL; } CGColorSpaceRelease( colorSpace ); return context; }
Listing 2-6 Drawing to a bitmap graphics context CGRect myBoundingBox; myBoundingBox = CGRectMake (0, 0, myWidth, myHeight); myBitmapContext = MyCreateBitmapContext (400, 300); // ********** Your drawing code here ********** 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 )); myImage = CGBitmapContextCreateImage (myBitmapContext); CGContextDrawImage(myContext, myBoundingBox, myImage); char *bitmapData = CGBitmapContextGetData(myBitmapContext); CGContextRelease (myBitmapContext); if (bitmapData) free(bitmapData); CGImageRelease(myImage);
支持的像素格式
表2-1总结了位图Graphics Context支持的像素格式,相关的颜色空间及像素格式支持的Mac OS X最早版本。像素格式用bpp(每像素的位数)和bpc(每个组件的位数)来表示。表格同时也包含与像素格式相关的位图信息常量。
表2-1:位图Graphics Context支持的像素格式
Null | 8 bpp, 8 bpc, kCGImageAlphaOnly |
Mac OS X, iOS |
Gray |
8 bpp, 8 bpc,kCGImageAlphaNone |
Mac OS X, iOS |
Gray |
8 bpp, 8 bpc,kCGImageAlphaOnly |
Mac OS X, iOS |
Gray |
16 bpp, 16 bpc, kCGImageAlphaNone |
Mac OS X |
Gray |
32 bpp, 32 bpc, kCGImageAlphaNone|kCGBitmapFloatComponents |
Mac OS X |
RGB |
16 bpp, 5 bpc, kCGImageAlphaNoneSkipFirst |
Mac OS X, iOS |
RGB |
32 bpp, 8 bpc, kCGImageAlphaNoneSkipFirst |
Mac OS X, iOS |
RGB |
32 bpp, 8 bpc, kCGImageAlphaNoneSkipLast |
Mac OS X, iOS |
RGB |
32 bpp, 8 bpc, kCGImageAlphaPremultipliedFirst |
Mac OS X, iOS |
RGB |
32 bpp, 8 bpc, kCGImageAlphaPremultipliedLast |
Mac OS X, iOS |
RGB |
64 bpp, 16 bpc, kCGImageAlphaPremultipliedLast |
Mac OS X |
RGB |
64 bpp, 16 bpc, kCGImageAlphaNoneSkipLast |
Mac OS X |
RGB |
128 bpp, 32 bpc, kCGImageAlphaNoneSkipLast |kCGBitmapFloatComponents |
Mac OS X |
RGB |
128 bpp, 32 bpc, kCGImageAlphaPremultipliedLast |kCGBitmapFloatComponents |
Mac OS X |
CMYK |
32 bpp, 8 bpc, kCGImageAlphaNone |
Mac OS X |
CMYK |
64 bpp, 16 bpc, kCGImageAlphaNone |
Mac OS X |
CMYK |
128 bpp, 32 bpc, kCGImageAlphaNone |kCGBitmapFloatComponents |
Mac OS X |
获取打印的Graphics Context
Mac OS X中的Cocoa应用程序通过自定义的NSView子类来实现打印。一个视图通过调用print:方法来进行打印。然后视图以打印机为目标创建一个Graphics Context,并调用drawRect:方法。应用程序使用与在屏幕进行绘制相同的绘制代码。我们同样可以自定义drawRect: 方法将图形绘制到打印机。
转自http://www.cocoachina.com/bbs/read.php?tid=78029。