首页 > 其他 > 详细

Quartz2D

时间:2014-02-24 18:30:04      阅读:398      评论:0      收藏:0      [点我收藏+]

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:

  bubuko.com,布布扣

  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.)

bubuko.com,布布扣
 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
View Code

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.

bubuko.com,布布扣
 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 }
View Code

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.

Quartz2D

原文:http://www.cnblogs.com/tekkaman/p/3562335.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!