IOS development-graph (CGContext), ioscgcontext

Source: Internet
Author: User

IOS development-graph (CGContext), ioscgcontext

  • Weekly
  • More
  • Login
IOS development-graph (CGContext) Time 09:17:43 CSDN blog OriginalHttp://blog.csdn.net/zhenyu5211314/article/details/24230581

0 CGContextRef context = UIGraphicsGetCurrentCont ext (); set the context
1 CGContextMoveToPoint
2 CGContextAddLineToPoint draw a straight line
4 CGContextAddEllipseInRec t draws an elliptic
4 CGContextSetLineCap
4 CGContextSetLineDash draw the dotted line
4 CGContextAddRect draw a box
4 CGContextStrokeRect: Specify the rectangle
4 CGContextStrokeRectWithW idth specifies the width of the rectangle line
4 CGContextStrokeLineSegme some straight lines
5 CGContextAddArc draws a curve. The first two stores are in the center. The center two stores start with radians. The last data is 0, and 1 is clockwise.
5 CGContextAddArcToPoint (context, 2, 9, 40); // draw two lines from point to point 1, and cut the circle from point 1 to point 2.
6 CGContextSetShadowWithCo lor
7 CGContextSetRGBFillColor: fill color
7 CGContextSetRGBStrokeCol or paint brush color settings
7 CGContextSetFillColorSpa ce color space filling
7 CGConextSetStrokeColorSp ace Color Space paint settings
8 CGContextFillRect supplement rect of current fill color
8 CGContextSetAlaha transparency
9 CGContextTranslateCTM change the canvas position
10 CGContextSetLineWidth: Set the line width
11 CGContextAddRects draw multiple lines
12 CGContextAddQuadCurveToP oint draw Curve
13 CGContextStrokePath
13 CGContextDrawPath
14 CGContextClosePath: closes the current line
15 CGContextTranslateCTM (context, 0, rect. size. height); CGContextScaleCTM (context, 1.0,-1.0); invert the canvas
16 CGContextSetInterpolatio nQuality background built-in color quality level
16 CGImageCreateWithImageIn Rect small image from the source Image
17. You can use the nsstring Plotting Method-(CGSize) drawInRect :( CGRect) rect withFont :( UIFont *) font lineBreakMode :( break) lineBreakMode alignment :( wait) alignment; To write the string.
18. The function of enlarging and downgrading images is slow.
UIGraphicsBeginImageCont ext (newSize );
UIImage * newImage = UIGraphicsGetImageFromCu rrentImageContext ();
UIGraphicsEndImageContex t ();
19 CGColorGetComponents () returns the directness and transparency of each color. Read-Only const float can be used to receive an array.
20 picture CGImageRef image = CGImageRetain (img. CGImage );
CGContextDrawImage (context, CGRectMake (10.0, height-
100.0, 90.0, 90.0), image );
21 implement the change-by-color Filling Method CGContextClip (context );
CGColorSpaceRef rgb = CGColorSpaceCreateDevice RGB ();
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 = CGGradientCreateWithColo rComponents
(Rgb, colors, NULL, sizeof (colors)/(sizeof (colors [0]) * 4 ));
CGColorSpaceRelease (rgb );
CGContextDrawLinearGradi ent (context, gradient, CGPointMake
(0.0, 0.0), CGPointMake (0.0, self. frame. size. height ),
KCGGradientDrawsBeforeSt artLocation );

22 Note: It is required
Use CGContextStrokePath to describe the line, that is, the shape.
Then, CGContextFillPath is used to fill the color in the shape.
When filling a path, the sub-paths in the path are independently filled.
If it is an overlapping path, it determines whether a vertex is filled. There are two rules:
1, nonzero winding number rule: non-zero round number rule. If a point is crossed from left to right, counter + 1, from right to left, counter-1, and finally, if the result is 0, it is not filled. If it is non-zero, it is filled.
2. even-odd rule: a parity rule. If a vertex is crossed, then + 1 is followed by an odd number, and the even number is not filled. It has nothing to do with the direction.
Function
Description
CGContextEOFillPath
Fill the current path with the parity rule
CGContextFillPath
Use a non-zero round number rule to fill the current path
CGContextFillRect
Fill in the specified rectangle
CGContextFillRects
Fill in the specified rectangle
CGContextFillEllipseInRe ct
Fill in the ellipse in the specified rectangle
CGContextDrawPath
The filling rules are determined by the two parameters. kCGPathFill indicates a non-zero round number rule, kCGPathEOFill indicates a parity rule, kCGPathFillStroke indicates filling, and kCGPathEOFillStroke indicates Line tracing, not filling
Set how to mix two colors when one color overwrites the other.
The default method is
Result = (alpha * foreground) + (1-alpha) * background
CGContextSetBlendMode: sets blend mode.
CGContextSaveGState: Save the blend mode.
CGContextRestoreGState: use this function to restore the blend mode before saving it.
CGContextSetBlendMode mixed colors

Http://www.cocoachina.com/bbs/read.php? Tid = 75122 & page = 1

========================================================== ======================================

Dotted Line

Functions are required to draw dotted lines:

CGContextSetLineDash

This function requires four parameters:

  • Context-Needless to say
  • Phase-Later
  • Lengths-Specify how the dotted line is drawn alternately. For details, refer to the example.
  • Count-Lengths array Length
  1. CGContextRef context = UIGraphicsGetCurrentContext ();
  2. CGContextBeginPath (context );
  3. CGContextSetLineWidth (context, 2.0 );
  4. CGContextSetStrokeColorWithColor (context, [UIColorwhiteColor]. CGColor );
  5. Float lengths [] = {10 };
  6. CGContextSetLineDash (context, 0, lengths, 2 );
  7. CGContextMoveToPoint (context, 10.0, 20.0 );
  8. CGContextAddLineToPoint (context, 310.0, 20.0 );
  9. CGContextStrokePath (context );
  10. CGContextClosePath (context );

The lengths value {10, 10} indicates that 10 points are first drawn, and then 10 points are skipped,

  • Float lengths [] = {10, 5 };
  • CGContextSetLineDash (context, 0, lengths, 2 );
  • CGContextMoveToPoint (context, 0.0, 20.0 );
  • CGContextAddLineToPoint (context, 310.0, 20.0 );
  • CGContextStrokePath (context );
  • CGContextSetLineDash (context, 5, lengths, 2 );
  • CGContextMoveToPoint (context, 0.0, 40.0 );
  • CGContextAddLineToPoint (context, 310.0, 40.0 );
  • CGContextStrokePath (context );
  • CGContextSetLineDash (context, 8, lengths, 2 );
  • CGContextMoveToPoint (context, 0.0, 60.0 );
  • CGContextAddLineToPoint (context, 310.0, 60 .);
  • CGContextStrokePath (context );
  • Display:


    Since the lengths value is {10, 5}, the first line is to draw 10, skip 5, and draw repeatedly.

    If the phase value of the second line is 5, first draw [10 minus 5], then skip 5, draw 10, and draw again.

    The third is also true. Draw 2 first and then skip 5.

    CGContextSetStrokeColorW ithColor (myContext, [UIColor blackColor]. CGColor );
    CGContextSetLineDash (myContext, phase, lengths, 2 );
    CGContextClosePath (myContext );
    CGContextStrokePath (myContext );

    ========================================================== ======================================

    Tangent

    -(Void) drawRect :( CGRect) rect {

    CGContextRef context = UIGraphicsGetCurrentContext ();

    CGContextSetLineWidth (context, 2.0 );

    CGContextSetStrokeColorWithColor (context, [UIColor blueColor]. CGColor );

    CGContextMoveToPoint (context, 100,100 );
    CGContextAddArcToPoint (context, 100,200,300,200,100 );
    CGContextStrokePath (context );
    }

    ========================================================== ======================================

    -(Void) drawRect :( CGRect) rect {

    CGContextRef context = UIGraphicsGetCurrentContext ();

    CGContextSetLineWidth (context, 2.0 );

    CGContextSetStrokeColorWithColor (context, [UIColor blueColor]. CGColor );

    CGRect rectangle = CGRectMake (60,170,200, 80 );

    CGContextAddEllipseInRect (context, rectangle );

    CGContextStrokePath (context );
    }

    ========================================================== ======================================

    -(Void) drawRect :( CGRect) rect {

    CGContextRef context = UIGraphicsGetCurrentContext ();

    CGContextSetLineWidth (context, 2.0 );

    CGContextSetStrokeColorWithColor (context, [UIColor blueColor]. CGColor );

    CGContextMoveToPoint (context, 10, 10 );

    CGContextAddCurveToPoint (context, 0, 50,300,250,300,400 );

    CGContextStrokePath (context );
    }

    ========================================================== ======================================

    -(Void) drawRect :( CGRect) rect {

    CGContextRef context = UIGraphicsGetCurrentContext ();

    CGContextSetLineWidth (context, 2.0 );

    CGContextSetStrokeColorWithColor (context, [UIColor blueColor]. CGColor );

    CGContextMoveToPoint (context, 10,200 );

    CGContextAddQuadCurveToPoint (context, 150, 10,300,200 );

    CGContextStrokePath (context );
    }

    ========================================================== ======================================

    -(Void) drawRect :( CGRect) rect {

    CGContextRef context = UIGraphicsGetCurrentContext ();

    CGContextSetLineWidth (context, 5.0 );

    CGContextSetStrokeColorWithColor (context, [UIColor blueColor]. CGColor );

    CGFloat dashArray [] = {2, 6, 4, 2 };

    CGContextSetLineDash (context, 3, dashArray, 4 );

    CGContextMoveToPoint (context, 10,200 );

    CGContextAddQuadCurveToPoint (context, 150, 10,300,200 );

    CGContextStrokePath (context );
    }

    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.