UIGraphicsGetCurrentContext
.函数来获取这个Graphics Context。
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
@implementation MyQuartzView - (id)initWithFrame:(NSRect)frameRect { self = [super initWithFrame:frameRect]; return self; } - (void)drawRect:(NSRect)rect { 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
CGContextRef MyPDFContextCreate (const CGRect *inMediaBox, CFStringRef path) { CGContextRef myOutContext = NULL; CFURLRef url; url = CFURLCreateWithFileSystemPath (NULL, // 1 path, kCFURLPOSIXPathStyle, false); if (url != NULL) { myOutContext = CGPDFContextCreateWithURL (url,// 2 inMediaBox, NULL); CFRelease(url);// 3 } return myOutContext;// 4 }
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; }
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);
UIGraphicsBeginImageContextWithOptions
取代Quartz低层函数。如果使用Quartz创建一下后台bitmap,bitmap Graphics Context使用的坐标系统是Quartz默认的坐标系统。而使用UIGraphicsBeginImageContextWithOptions创建图形上下文,UIKit将会对坐标系统使用与UIView对象的图形上下文一样的转换。这允许应用程序使用相同的绘制代码而不需要担心坐标系统问题。虽然我们的应用程序可以手动调整CTM达到相同的效果,但这种做没有任何好处。
CGContextRef MyCreateBitmapContext (int pixelsWide, int pixelsHigh) { CGContextRef context = NULL; CGColorSpaceRef colorSpace; void * bitmapData; int bitmapByteCount; int bitmapBytesPerRow; bitmapBytesPerRow = (pixelsWide * 4);// 1 bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);// 2 bitmapData = calloc( bitmapByteCount );// 3 if (bitmapData == NULL) { fprintf (stderr, "Memory not allocated!"); returnNULL; } context = CGBitmapContextCreate (bitmapData,// 4 pixelsWide, pixelsHigh, 8, // bits per component bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); if (context== NULL) { free (bitmapData);// 5 fprintf (stderr, "Context not created!"); returnNULL; } CGColorSpaceRelease( colorSpace );// 6 return context;// 7 }具体解释:
CGRect myBoundingBox;// 1 myBoundingBox = CGRectMake (0, 0, myWidth, myHeight);// 2 myBitmapContext = MyCreateBitmapContext (400, 300);// 3 // ********** Your drawing code here ********** // 4 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);// 5 CGContextDrawImage(myContext, myBoundingBox, myImage);// 6 char *bitmapData = CGBitmapContextGetData(myBitmapContext); // 7 CGContextRelease (myBitmapContext);// 8 if (bitmapData) free(bitmapData); // 9 CGImageRelease(myImage);
CS |
Pixel format and bitmap information constant |
Availability |
---|---|---|
Null |
8 bpp, 8 bpc, |
Mac OS X, iOS |
Gray |
8 bpp, 8 bpc, |
Mac OS X, iOS |
Gray |
8 bpp, 8 bpc, |
Mac OS X, iOS |
Gray |
16 bpp, 16 bpc, |
Mac OS X |
Gray |
32 bpp, 32 bpc, |
Mac OS X |
RGB |
16 bpp, 5 bpc, |
Mac OS X, iOS |
RGB |
32 bpp, 8 bpc, |
Mac OS X, iOS |
RGB |
32 bpp, 8 bpc, |
Mac OS X, iOS |
RGB |
32 bpp, 8 bpc, |
Mac OS X, iOS |
RGB |
32 bpp, 8 bpc, |
Mac OS X, iOS |
RGB |
64 bpp, 16 bpc, |
Mac OS X |
RGB |
64 bpp, 16 bpc, |
Mac OS X |
RGB |
128 bpp, 32 bpc, |
Mac OS X |
RGB |
128 bpp, 32 bpc, |
Mac OS X |
CMYK |
32 bpp, 8 bpc, |
Mac OS X |
CMYK |
64 bpp, 16 bpc, |
Mac OS X |
CMYK |
128 bpp, 32 bpc, |
Mac OS X |