# Three steps of the Quatre 2D drawing function (context, drawing, rendering), quatre2d

Source: Internet
Author: User

Three steps of the Quatre 2D drawing function (context, drawing, rendering), quatre2d

1. How does qurza2d draw drawing information and drawing attributes to the drawing context?

Note:

Create a new project, customize a view class and storyboard Association, and override the drowrect method in this class. Three steps to draw a line: (1) Get the context (2) Drawing (3) Rendering requirements: draw two separate line codes and:
`1-(void) drawRect :( CGRect) rect 2 {3 // get context 4 CGContextRef ctx = UIGraphicsGetCurrentContext (); 5 // plot 6 // The first line 7 CGContextMoveToPoint (ctx, 20,100); 8 CGContextAddLineToPoint (ctx, 100,320); 9 10 // second line 11 CGContextMoveToPoint (ctx, 40,200); 12 CGContextAddLineToPoint (ctx, 80,100 ); 13 // render 14 CGContextStrokePath (ctx); 15 16}`
: Set the width of a line segment: the two ends are circular, color, and so on. Code and (the second line is also rendered as the style and state of the first line)
`1-(void) drawRect :( CGRect) rect 2 {3 // get context 4 CGContextRef ctx = UIGraphicsGetCurrentContext (); 5 // plot 6 // The first line 7 CGContextMoveToPoint (ctx, 20,100); 8 CGContextAddLineToPoint (ctx, 100,320); 9 10 // set the status of the first line 11 // set the width of the line 12 CGContextSetLineWidth (ctx, 12 ); 13 // set the line color 14 [[UIColor brownColor] set]; 15 // set the style at both ends of the line to rounded corner 16 CGContextSetLineCap (ctx, kCGLineCapRound ); 17 // render the Lines 18 CGContextStrokePath (ctx); 19 20 // The second line 21 CGContextMoveToPoint (ctx, 40,200); 22 CGContextAddLineToPoint (ctx, 80,100 ); 23 // render 24 CGContextStrokePath (ctx); 25 26}`

:

New requirement: to make the two lines different in color, the second line should be changed to the original version. To meet the above requirements, we have the following practices:

First approach: when setting the second line, clear its status
`1-(void) drawRect :( CGRect) rect 2 {3 // get context 4 CGContextRef ctx = UIGraphicsGetCurrentContext (); 5 // plot 6 // The first line 7 CGContextMoveToPoint (ctx, 20,100); 8 CGContextAddLineToPoint (ctx, 100,320); 9 10 // set the status of the first line 11 // set the width of the line 12 CGContextSetLineWidth (ctx, 12 ); 13 // set the line color 14 [[UIColor brownColor] set]; 15 // set the style at both ends of the line to rounded corner 16 CGContextSetLineCap (ctx, kCGLineCapRound ); 17 // render the Lines 18 CGContextStrokePath (ctx); 19 20 // The second line 21 CGContextMoveToPoint (ctx, 40,200); 22 CGContextAddLineToPoint (ctx, 80,100 ); 23 24 // clear status 25 CGContextSetLineWidth (ctx, 1); 26 [[UIColor blackColor] set]; 27 CGContextSetLineCap (ctx, kCGLineCapButt ); 28 29 // rendering 30 CGContextStrokePath (ctx); 31 32}`
Method 2: Cut the first line from the beginning to the rendering code to the second line after rendering, so that the first line is drawn and rendered first, this line has not been set for the drawing information, and the second line displayed is the default effect of the system.
`1-(void) drawRect :( CGRect) rect 2 {3 // get context 4 CGContextRef ctx = UIGraphicsGetCurrentContext (); 5 // plot 6 7 // The second line 8 CGContextMoveToPoint (ctx, 40,200); 9 CGContextAddLineToPoint (ctx, 80,100); 10 11 // clear status 12 // CGContextSetLineWidth (ctx, 1); 13 // [[UIColor blackColor] set]; 14 15 // CGContextSetLineCap (ctx, kCGLineCapButt); 16 17 // rendering 18 CGContextStrokePath (ctx); 19 20 // line 1 21 CGContextMoveToPoint (ctx, 20,100 ); 22 CGContextAddLineToPoint (ctx, 100,320); 23 24 // set the status of the first line 25 // set the line width 26 CGContextSetLineWidth (ctx, 12 ); 27 // set the line color 28 [[UIColor brownColor] set]; 29 // set the style at both ends of the line to rounded corner 30 CGContextSetLineCap (ctx, kCGLineCapRound ); 31 // render the line 32 CGContextStrokePath (ctx); 33}`

The two methods have the same effect:

However, in some cases, you must first draw the first line and then draw the second line, which must be in the cross section and the second line above the first line. If this is the case, you can only use the first method. However, if you have new requirements and want to draw two more lines on this basis, you need to clear the status of ctx many times, very troublesome. To solve this problem, we will introduce the graphic context stack. 2. The complete plotting procedure is started to display the custom view. When the program is displayed in front of us for the first time, the program will call the drawRect: method, get the graphic context in it (in memory), and then save the drawing information using the graphic context, it can be understood that there is an area in the graphic context to save the drawing information, and an area to save the drawing state (width, rounded corner, color ). A straight line is not directly drawn to the view. It can be understood that there is a separate area in the graphic context to draw the image first. When the rendering method is called, then, display the drawn image to the view. In the drawing area, the status information (line width, rounded corner, color) of the drawing state area is saved, and the first line is drawn in the drawing area. In fact, the straight line has been drawn in the graphic area before rendering. Note: These and the program code blocks in this Article do not have a one-to-one correspondence, just to illustrate the complete process of drawing. When calling the rendering method, we can directly display the image that has been drawn in the graphic area to the view, which is what we see. If you do not reset the drawing status when you draw the second image, you can find that the drawing status used when you draw the first image is saved in the drawing context, before rendering the second line, the second line is set based on the first line (the previous drawing Status). After rendering, the second line is displayed on the screen. Reference code:
`1-(void) drawRect :( CGRect) rect 2 {3 // get context 4 CGContextRef ctx = UIGraphicsGetCurrentContext (); 5 // plot 6 // The first line 7 CGContextMoveToPoint (ctx, 20,100); 8 CGContextAddLineToPoint (ctx, 100,320); 9 10 // set the status of the first line 11 // set the width of the line 12 CGContextSetLineWidth (ctx, 12 ); 13 // set the line color 14 [[UIColor brownColor] set]; 15 // set the style at both ends of the line to rounded corner 16 CGContextSetLineCap (ctx, kCGLineCapRound ); 17 // render the Lines 18 CGContextStrokePath (ctx); 19 20 // The second line 21 CGContextMoveToPoint (ctx, 40,200); 22 CGContextAddLineToPoint (ctx, 80,100 ); 23 // render 24 CGContextStrokePath (ctx); 25}`
If the status is cleared, the current drawing information (changed -- cleared) will be searched when the second line is drawn in the drawing area before rendering ), draw the second line based on the drawing information, and display the second line to the view when calling the rendering method. Reference code:
`1-(void) drawRect :( CGRect) rect 2 {3 // get context 4 CGContextRef ctx = UIGraphicsGetCurrentContext (); 5 // plot 6 // The first line 7 CGContextMoveToPoint (ctx, 20,100); 8 CGContextAddLineToPoint (ctx, 100,320); 9 10 // set the status of the first line 11 // set the width of the line 12 CGContextSetLineWidth (ctx, 12 ); 13 // set the line color 14 [[UIColor brownColor] set]; 15 // set the style at both ends of the line to rounded corner 16 CGContextSetLineCap (ctx, kCGLineCapRound ); 17 // render the Lines 18 CGContextStrokePath (ctx); 19 20 // The second line 21 CGContextMoveToPoint (ctx, 40,200); 22 CGContextAddLineToPoint (ctx, 80,100 ); 23 24 // clear status 25 CGContextSetLineWidth (ctx, 1); 26 [[UIColor blackColor] set]; 27 CGContextSetLineCap (ctx, kCGLineCapButt ); 28 29 // rendering 30 CGContextStrokePath (ctx); 31}`

3. Graphic context stack 1. A brief description of the image Context

CGContextSaveGState (ctx );

Method to copy the obtained context and save the purest image context. Before drawing the second line, use the CGContextRestoreGState (ctx); Method to restore the original graph context. Code:
`1-(void) drawRect :( CGRect) rect 2 {3 // get context 4 CGContextRef ctx = UIGraphicsGetCurrentContext (); 5 // save an original graph context 6 CGContextSaveGState (ctx); 7 8 // plot 9 // The first line 10 CGContextMoveToPoint (ctx, 20,100); 11 CGContextAddLineToPoint (ctx, 100,320); 12 13 // set the status of the first line 14 // set the line width 15 CGContextSetLineWidth (ctx, 12 ); 16 // set the line color 17 [[UIColor brownColor] set]; 18 // set the style at both ends of the line to the rounded corner 19 CGContextSetLineCap (ctx, kCGLineCapRound ); 20 // render the line 21 CGContextStrokePath (ctx); 22 23 // restore the purest graph context saved at the beginning 24 CGContextRestoreGState (ctx ); 25 // The second line 26 CGContextMoveToPoint (ctx, 40,200); 27 CGContextAddLineToPoint (ctx, 80,100); 28 29 // clear status 30 // CGContextSetLineWidth (ctx, 1 ); 31 // [[UIColor blackColor] set]; 32 // CGContextSetLineCap (ctx, kCGLineCapButt); 33 34 // render 35 CGContextStrokePath (ctx); 36}`
2. When the graphic context stack mechanism draws the first line, it copies the current graphic context and saves it to the graphic context stack. When drawing the second line, extract the drawing information at the top of the stack from the graph context stack. As the status information of the second line, the status information of the second line is also based on this (the image context originally saved). Note: If the stack is saved several times, it can be retrieved several times (for example, it cannot be saved once or twice, if it is empty in the stack, it will be directly suspended ).

Related Keywords:
Related Article

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.