15. Quartz2D for ios, 15. iosquartz2d
1. What is Quartz2D?
• Quartz2D is a two-dimensional Drawing Engine that supports both iOS and Mac systems • Quartz 2D work drawing: draw lines, triangles, rectangles, circles, arcs, and other words draw \ generate an image (image) read \ generate PDF \ crop an image custom UI control
2. Custom View
Graphic Context
• Graphics Context: A CGContextRef type data
• The role of the graphic context stores the drawing information and the drawing status determines the output target of the drawing (where is the drawing going ?)
(The output target can be a PDF file, Bitmap, or display window)
DrawRect: context obtained in
• After obtaining the context in the drawRect: method, you can draw things to the view.
• View has a layer attribute. drawRect: The method obtains a Layer Graphics Context. Therefore, the painting is actually drawn to the view layer.
• View can display things completely because of its internal layer
Quartz2D drawing code steps
Obtain the graphic context CGContextRef ctx = UIGraphicsGetCurrentContext (); splicing path (the following code is a line segment) CGContextMoveToPoint (ctx, 10, 10); CGContextAddLineToPoint (ctx, 100,100 ); draw the path CGContextStrokePath (ctx); // CGContextFillPath (ctx );
Common splicing path Functions
Create a start void CGContextMoveToPoint (CGContextRef c, CGFloat x, CGFloat y) To add a new line segment to a certain point void CGContextAddLineToPoint (CGContextRef c, CGFloat x, CGFloat y) add a rectangle void CGContextAddRect (CGContextRef c, CGRect rect) add an elliptical void vertex (CGContextRef context, CGRect rect) add an arc void vertex (CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)
Common plot path Functions
The Mode parameter determines the rendering mode void CGContextDrawPath (CGContextRef c, quiet Mode). The void CGContextStrokePath (CGContextRef c) indicates the solid path void CGContextFillPath (CGContextRef c: generally, functions starting with CGContextDraw, CGContextStroke, and CGContextFill are used to draw paths.
Image context stack operations
Copy the current context and save it to the top of the stack (the stack is called the "graphic context stack"). void CGContextSaveGState (CGContextRef c) pulls the context at the top of the stack out of the stack, replace the current context void CGContextRestoreGState (CGContextRef c)
Matrix Operations
Using Matrix Operations, You can scale void CGContextScaleCTM (CGContextRef c, CGFloat sx, CGFloat sy) to rotate void CGContextRotateCTM (CGContextRef c, CGFloat angle) translation void CGContextTranslateCTM (CGContextRef c, CGFloat tx, CGFloat ty)
Image Watermark
Enable a bitmap-based image context void uigraphicsbeginimagecontextwitexceptions (CGSize size, BOOL opaque, CGFloat scale) to obtain the image (UIImage) UIImage * watermark () from the context (); end the bitmap-based image context void UIGraphicsEndImageContext ();
Crop Images
Void CGContextClip (CGContextRef c) crops the path drawn up and down currently (beyond this cropping area cannot be displayed)
Screen
-(Void) renderInContext :( CGContextRef) ctx; call the renderInContext of a view layer: method.
Download Quartz 2D official documentation