Quartz2D, quartz

Source: Internet
Author: User

Quartz2D, quartz
1. Quartz2D Overview

Quartz2D is a two-dimensional Drawing Engine that supports both Mac and iOS systems.

Quartz2D can draw graphics, draw text, draw images, generate PDF, crop images, etc. You can also use custom UI controls.

For example, the canvas application, gesture unlocking function, and graphical report are all implemented using zz2d.

1. Graphics Context)

Graphics Context is a CGContextRef type data, it is used to save drawing information, drawing status, and determine the Drawing Output target (the output target can be a PDF file, Bitmap, or display window ).

For the same set of drawing sequences, you can specify different image contexts to draw the same image to different targets.

Quartz2D provides several types of Graphics Context:

  • Bitmap Graphics Context
  • PDF Graphics Context
  • Window Graphics Context
  • Layer Graphics Context
  • Printer Graphics Context
2. drawRect: Method

When a view is displayed on the screen for the first time (displayed on UIWindow), drawRect is called. When setNeedDisplay or setNeedDisplayInRect: Of the view is called, drawRect is also called:

3. drawRect: Get context in

After obtaining the context in the drawRect: method, you can draw a picture to the view. View has a layer attribute. drawRect: A Layer Graphics Context is obtained in the method. Therefore, the drawing is actually drawn to the view layer.

The reason why a view can display things is that it has a layer.

4. Quartz2D Memory Management

Objects Created using functions that contain "Create" or "Copy" must be released after use; otherwise, memory leakage may occur, you do not need to release an object obtained by using a function that does not include "Create" or "Copy.

If an object is retained, you need to release it when it is no longer in use.

You can use the Quartz2D function to specify the retain and release objects. For example, if you create a CGColorSpace object, use the CGColorSpaceRetain and CGColorSpaceRelease functions to retain and release objects.

You can also use the CFRetain and CFRelease of the Core Foundation. Note that NULL values cannot be passed to these functions.

 

Ii. Code 1. Draw a triangle
1/** 2 * draw triangle 3 */4 void drawTriangle () 5 {6 // 1. obtain the graph context 7 CGContextRef contextRef = UIGraphicsGetCurrentContext (); 8 9 // 2. concatenate the graph path 10 // set a starting point 11 CGContextMoveToPoint (contextRef, 100,100); 12 // Add a line segment to (100,100) 13 CGContextAddLineToPoint (contextRef, 200,200 ); 14 // Add a line segment to (150) 15 CGContextAddLineToPoint (contextRef, 40); 16 // close the path (connection start point and end point) 17 CGContextClosePath (contextRef ); 18 19 // 3. rendering to View 20 CGContextStrokePath (contextRef); 21}
2. Draw a rectangle
1/** 2 * draw quadrilateral 3 */4 void drawQuadrilateral () 5 {6 // 1. obtain the graphic context 7 CGContextRef contextRef = UIGraphicsGetCurrentContext (); 8 // 2. draw a rectangle 9 CGContextAddRect (contextRef, CGRectMake (100,100,150,100); 10 // 3. drawing Graphics 11 CGContextStrokePath (contextRef); 12}
3. Circle
1/** 2 * circle 3 */4 void drawCircle () 5 {6 // 1. obtain the graphic context 7 CGContextRef contextRef = UIGraphicsGetCurrentContext (); 8 // 2. circle 9 CGContextAddEllipseInRect (contextRef, CGRectMake (100,100,100,100); 10 // 3. drawing Graphics 11 CGContextFillPath (contextRef); 12}
4. Draw an arc
1/** 2 * draw an arc 3 */4 void drawArc () 5 {6 // 1. obtain the graph context 7 CGContextRef contextRef = UIGraphicsGetCurrentContext (); 8 9 // 2. draw an arc 10 // x \ y: Center Coordinate 11 // radius: radius 12 // startAngle: Start angle 13 // endAngle: end angle 14 // clockwise: stretch direction of the arc (0: clockwise, 1: counterclockwise) 15 CGContextAddArc (contextRef, 100,100, 50, 0, M_PI, 1); 16 17 // 3. drawing Graphics 18 CGContextStrokePath (contextRef); 19}
5. Draw text
1/** 2 * draw Text 3 */4 void drawText () 5 {6 NSString * str = @ "Draw text "; 7 // set the text attribute 8 NSMutableDictionary * attrs = [invalid dictionary]; 9 attrs [identifier] = [UIColor redColor]; 10 attrs [NSFontAttributeName] = [UIFont systemFontOfSize: 20]; 11 12 // draw the text in a certain position 13 [str drawAtPoint: CGPointMake (100,100) withAttributes: attrs]; 14 15 // draw the text in a rectangle 16 // [str drawInRect: CGRectMake (100,100,100,100) withAttributes: attrs]; 17}
6. Draw Images
1/** 2 * picture 3 */4 void drawImage () 5 {6 UIImage * image = [UIImage imageNamed: @ "1.jpg"]; 7 [image drawAsPatternInRect: CGRectMake (100,100,100,100)]; 8}
7. Graphic context Stack
1/** 2 * graphic context stack 3 */4 void ContextSaveGState () 5 {6 // obtain the graphic context 7 CGContextRef contextRef = UIGraphicsGetCurrentContext (); 8 9 // copy a copy of contextRef to the stack 10 CGContextSaveGState (contextRef); 11 12 // set the drawing status 13 CGContextSetLineWidth (contextRef, 10 ); 14 [[UIColor redColor] set]; 15 CGContextSetLineCap (contextRef, latency); 16 17 // The first line 18 CGContextMoveToPoint (contextRef, 50, 50); 19 CGContextAddLineToPoint (contextRef, 120,190); 20 21 // draw line 22 CGContextStrokePath (contextRef); 23 24 // exit the context at the top of the stack and replace the current context 25 CGContextRestoreGState (contextRef ); 26 27 // second line 28 CGContextMoveToPoint (contextRef, 10, 70); 29 CGContextAddLineToPoint (contextRef, 220,290); 30 31 // line 32 CGContextStrokePath (contextRef); 33}
8. Matrix Operations (overall scaling, movement, rotation, etc)
1/** 2 * matrix operation 3 */4 void contextMatrix () 5 {6 // obtain the graphic context 7 CGContextRef contextRef = UIGraphicsGetCurrentContext (); 8 9 // reduce by 0.5 times (set before painting) 10 CGContextScaleCTM (contextRef, 0.5, 0.5); 11 // rotate 12 CGContextRotateCTM (contextRef, M_PI_4 ); 13 // move 14 CGContextTranslateCTM (contextRef, 0,150); 15 16 // draw a rectangle 17 CGContextAddRect (contextRef, CGRectMake (100,100,100,100); 18 CGContextStrokePath (contextRef); 19}
9. Crop Images
1/** 2 * crop an image 3 */4-(void) imageClip 5 {6 // 1. load the source image 7 UIImage * oldImage = [UIImage imageNamed: @ "1.jpg"]; 8 // 2. enable Context 9 uigraphicsbeginimagecontextwitexceptions (oldImage. size, NO, 0.0); 10 // 3. obtain the current context 11 CGContextRef contextRef = UIGraphicsGetCurrentContext (); 12 // 4. circle 13 CGRect circleRect = CGRectMake (0, 0, oldImage. size. width, oldImage. size. height); 14 CGContextAddEllipseInRect (contextRef, circleRect); 15 // 5. crop according to the current path shape (Circular). content beyond this shape is not displayed 16 CGContextClip (contextRef); 17 // 6. drawing 18 [oldImage drawInRect: circleRect]; 19 // 7. figure 20 UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); 21 // 8. end 22 UIGraphicsEndImageContext (); 23}
10. screenshots
1/** 2 * screenshot 3 */4-(void) screenCut 5 {6 // 1. enable context 7 uigraphicsbeginimagecontextwitexceptions (self. frame. size, NO, 0.0); 8 // 2. render the layer of the controller view to context 9 [self. layer renderInContext: UIGraphicsGetCurrentContext ()]; 10 // 3. retrieve image 11 UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); 12 // 4. end context 13 UIGraphicsEndImageContext (); 14}
11. redraw
1/** 2 * re-paint 3 */4-(void) reDraw5 {6 // re-paint (this method will re-call drawRect internally: Method for painting) 7 [self setNeedsDisplay]; 8}
12. common attributes
1/** 2 * common attributes 3 */4 void ContextProperty () 5 {6 // obtain the graphic context 7 CGContextRef contextRef = UIGraphicsGetCurrentContext (); 8 9 // set the line segment width to 10 CGContextSetLineWidth (contextRef, 10); 11 // set the color to 12 CGContextSetRGBStrokeColor (contextRef, 1, 0, 0, 1 ); 13 // set the line segment header tail style 14 CGContextSetLineCap (contextRef, kCGLineCapRound); 15 // set the line segment turning point style 16 CGContextSetLineJoin (contextRef, kCGLineJoinRound); 17 // set: at the same time, set it to solid and hollow color 18 // setStroke: set hollow color 19 // setFill: set solid color 20 [[UIColor whiteColor] set]; 21 22 // crop, the cropped content can only display 23 CGContextClip (contextRef); 24}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.