[Sharing] iOS Development-A Brief Note on CGContext


0 CGContextRef context = UIGraphics GetCurrentContext (); Setting context

1 CGContextMoveToPoint begins to draw lines

2 CGContextAddLineToPoint Draws Lines

Draw an ellipse with 4CGContextAdd Ellipse InRect

4 CGContextSetLineCap Sets the end shape of the line

4 CGContextSetLineDash draws dotted lines

4 CGContextAddRect Draw a Box

4 CGContextStrokeRect Specifies Rectangles

4 CGContextStrokeRectWithWidth Specifies Rectangular Line Width

4 CGContextStrokeLine Segments Some Lines

5CGContextAddArc draws a curve with the first two stores as the center and the middle two stores as the starting arc. The last data is 0, clockwise and 1 counter-clockwise.

CGContextAddArcToPoint (context, 0, 0, 2, 9, 40); // Draw two lines from point to point, and cut the circle from point 1 to point 2.

6 CGContextSetShadowWithColor Settings Shadow

7 CGContextSetRGBFillColor This Fill Color

7 CGContextSetRGBStrokeColor Brush Color Settings

7 CGContextSetFillColorSpace Color Space Filling

7 CGConextSetStrokeColorSpace Color Space Brush Settings

8 CGContextFillRect complements rect of the current fill color

8 CGContextSetAlaha Transparency

9 CGContextTranslateCTM Change Canvas Position

10 CGContextSetLineWidth Settings Width Width

11 CGContextAddRects Draw Multiple Lines

12 CGContextAddQuadCurve ToPoint Drawing Curve

13 CGContextStrokePath begins to draw pictures

13 CGContextDrawPath Sets Drawing Mode

14 CGContextClosePath closes the current line

15 CGContextTranslateCTM (context, 0, rect. size. height); CGContextScale CTM (context, 1.0, -1.0); inversion canvas

16 CGContextSet Interpolation Quality Background Built-in Color Quality Level

16 CGImageCreateWithImageInRect Gets Small Pictures from the Original Picture

Write 17 strings using nsstring’s own drawing method

-(CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment;

Just write it in.

The 18-pair zoom-in and zoom-out feature is just a little slower.

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

19 CGColorGetComponents () Returns the color’s straightness and transparency, which can be received as an array by a read-only const float.

20 pictures

CGImageRef image=CGImageRetain(img.CGImage);
CGContextDrawImage(context, CGRectMake(10.0, height - 100.0, 90.0, 90.0), image);

21 Implementation of Gradual Color Filling Method

CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
        204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00,

        29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00,

        0.0 / 255.0,  50.0 / 255.0, 126.0 / 255.0, 1.00,
CGGradientRef gradient = CGGradientCreateWithColorComponents       
(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
CGContextDrawLinearGradient(context, gradient,CGPointMake    
(0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),                    

22 Notes: After drawing, you must

  • First use CGContextStrokePath to draw lines, that is, shapes.

  • Then CGContextFillPath is used to fill the color in the shape.

When filling a path, the sub-paths in the path are filled independently.
If the overlapping paths determine whether a point is filled, there are two rules

1, nonzero winding number rule: non-zero winding number rule: if a point is crossed from left to right, counter + 1, right to left, counter-1, finally, if the result is 0, then no filling, if it is non-zero, then filling.

2, even-odd rule: even-odd rule, if a point is crossed, then + 1, and finally odd number, then to be filled, even number is not filled, and has nothing to do with direction.



Fill in the current path with parity rules

Fill in the current path with non-zero winding rules

Fill in the specified rectangle

Fill in some specified rectangles

Fill in the ellipse in the specified rectangle


Two parameters determine the filling rule. kCGPathFill denotes non-zero winding rule, kCGPathEOFill denotes parity rule, kCGPathFillStroke denotes filling, kCGPathEOFILStroke denotes tracing, not filling.

Set how two colors mix when one color is covered with another
The default is

result = (alpha foreground) + (1 – alpha) background

CGContextSetBlendMode: Set blend mode.

CGContextSaveGState: Save blend mode.

CGContextRestoreGState: Restore blend mode with this function before saving.

CGContextSetBlendMode Mixed Two Colors

Shared sources:

Recommended Today

The construction tool of C ා

What’s the motivation for writing this article? Why do you want to study the build tool (see. Csproj), create a new C project with visual studio IDE, and click build to run it? A: please refer to this_minutes_before_coding. I call it “engineering.”. Engineering development is not just about opening IDE and creating new projects. It […]