【Quartz2D】
Quartz 2D is a two-dimensional drawing engine. Quartz 2D is resolution- and device-independent; you don’t need to think about the final destination when you use the Quartz 2D application programming interface (API) for drawing.
The Quartz 2D API is part of the Core Graphics framework, so you may see Quartz referred to as Core Graphics or, simply, CG.
【Graphics Contexts】
A graphics context represents a drawing destination. It contains drawing parameters and all device-specific information that the drawing system needs to perform any subsequent drawing commands.
A graphics context is represented by CGContextRef, which is an opaque data type.
【Window Graphics Contexts】
You obtain this graphics context in your drawRect: method by calling the UIKit function UIGraphicsGetCurrentContext in method
drawRect:.
In Mac You obtain a Quartz graphics context from within the drawRect: routine of a Cocoa application using the following line of code:
The method currentContext returns the NSGraphicsContext instance of the current thread. The method graphicsPort returns the low-level, platform-specific graphics context represented by the receiver, which is a Quartz graphics context. (Don’t get confused by the method names; they are historical.)
1 @implementation MyQuartzView 2 3 - (id)initWithFrame:(NSRect)frameRect 4 { 5 self = [super initWithFrame:frameRect]; 6 return self; 7 } 8 9 - (void)drawRect:(NSRect)rect 10 { 11 CGContextRef myContext = [[NSGraphicsContext // 1 12 currentContext] graphicsPort]; 13 // ********** Your drawing code here ********** // 2 14 CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);// 3 15 CGContextFillRect (myContext, CGRectMake (0, 0, 200, 100 ));// 4 16 CGContextSetRGBFillColor (myContext, 0, 0, 1, .5);// 5 17 CGContextFillRect (myContext, CGRectMake (0, 0, 100, 200));// 6 18 } 19 20 @end
【Creating a Bitmap Graphics Context】
A bitmap graphics context accepts a pointer to a memory buffer that contains storage space for the bitmap. When you paint into the bitmap graphics context, the buffer is updated. After you release the graphics context, you have a fully updated bitmap in the pixel format you specify.
1 CGContextRef MyCreateBitmapContext (int pixelsWide, 2 int pixelsHigh) 3 { 4 CGContextRef context = NULL; 5 CGColorSpaceRef colorSpace; 6 void * bitmapData; 7 int bitmapByteCount; 8 int bitmapBytesPerRow; 9 10 bitmapBytesPerRow = (pixelsWide * 4);// 1 11 bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); 12 13 colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);// 2 14 bitmapData = calloc( bitmapByteCount );// 3 15 if (bitmapData == NULL) 16 { 17 fprintf (stderr, "Memory not allocated!"); 18 return NULL; 19 } 20 context = CGBitmapContextCreate (bitmapData,// 4 21 pixelsWide, 22 pixelsHigh, 23 8, // bits per component 24 bitmapBytesPerRow, 25 colorSpace, 26 kCGImageAlphaPremultipliedLast); 27 if (context== NULL) 28 { 29 free (bitmapData);// 5 30 fprintf (stderr, "Context not created!"); 31 return NULL; 32 } 33 CGColorSpaceRelease( colorSpace );// 6 34 35 return context;// 7 36 }
【Anti-Aliasing】
Bitmap graphics contexts support anti-aliasing. You can turn anti-aliasing off for a particular bitmap graphics context by calling the function CGContextSetShouldAntialias. The anti-aliasing setting is part of the graphics state.
You can control whether to allow anti-aliasing for a particular graphics context by using the function CGContextSetAllowsAntialiasing. Pass true to this function to allow anti-aliasing; false not to allow it. This setting is not part of the graphics state. Quartz performs anti-aliasing when the context and the graphic state settings are set to true.
原文:http://www.cnblogs.com/tekkaman/p/3562335.html