Quartz 2D Simple Drawing

Source: Internet
Author: User

    1. To draw with the CG framework:
    2. 1. Subclasses of UIView
    3. 2. Replication DrawRect method in Graphicview
    4. 3. Get the Graphics context object
    5. 4. Draw lines
    6. (1) First create a path, the line is defined in this path, and then the path to the context to display.
    7. (2) Draw the line directly to the context.
    8. 5. Draw Simple Graphics
    9. 6. Drawing text

10.7. Drawing an image

12.-(void) DrawRect: (cgrect) Rect {

13.//1. Getting the graphics context

    1. Cgcontextref context = Uigraphicsgetcurrentcontext ();//Graphics contexts
    2. [Self drawimage:context];

17.}

19.////Drawing Line Method one

20.//2. Creating a drawing Path

    1. Cgmutablepathref path = cgpathcreatemutable ();

22.//add a starting point on the path

    1. Cgpathmovetopoint (Path, NULL, 50, 50);
    2. Cgpathaddlinetopoint (Path, NULL, 100, 100);

25.//closes the path when the drawing is complete, which connects the start and end points

    1. Cgpathclosesubpath (path);

27.//3. Adding a path to a graphics context

    1. Cgcontextaddpath (context, path);

29.//4. Setting properties for a graphics context

    1. Set line width
    2. Cgcontextsetlinewidth (context, 5);
    3. Set Line Color
    4. Cgcontextsetrgbstrokecolor (context, 254.0/255, 201.0/255, 21.0/255, 1);
    5. Fill Color
    6. Cgcontextsetrgbfillcolor (context, 0, 1, 0, 1);

36.//5. Drawing a path on a graphics context

    1. /*
    2. Drawing mode:
    3. Kcgpathfill: Filled (solid)
    4. Kcgpathstroke: Draw Line (hollow)
    5. Kcgpathfillstroke: Draw line and fill
    6. */
    7. Cgcontextdrawpath (context, kcgpathfillstroke);

44.//6. Release path, there is a create will have release

    1. Cgpathrelease (path);

47.////Draw Line Method two

48.CGPoint points[] = {{50,50},{100,100},{50,100},{50,50}};

    1. Cgcontextaddlines (context, points, 4);

50.//Setting Line Colors

    1. [[Uicolor Redcolor] setstroke];

52.//Setting the Fill color

    1. [[Uicolor Bluecolor] setfill];

54.//setting line and fill colors at the same time

    1. [[Uicolor Redcolor] set];

56.CGContextDrawPath (context, kcgpathfillstroke);

58.//drawing rectangles

59.//1. Using the functions provided by CG

    1. CGRect rect = CGRectMake (10, 10, 100, 200);
    2. Add to Graph context
    3. Cgcontextaddrect (context, rect);
    4. Setting the properties of a context
    5. [[Uicolor Redcolor] set];
    6. Cgcontextdrawpath (context, kcgpathfillstroke);

66.//2. Functions provided using the Uikit framework

    1. CGRect rect = CGRectMake (10, 10, 100, 200);
    2. Setting the properties of a context
    3. [[Uicolor Redcolor] set];
    4. Draw a filled rectangle
    5. Uirectfill (rect);
    6. Draw a hollow rectangle
    7. Uirectframe (rect);

76.//Draw a circle

77.//1.

    1. coordinates of x, y Center
    2. Radius: Radius
    3. Startangel: Starting angle
    4. Endangel: End of angle
    5. clockwise:0 Clockwise, 1 counterclockwise
    6. Cgcontextaddarc (context, 0, m_pi_4, 0);

84.//2. Given a rectangle, draw a inscribed circle

    1. CGRect rect = CGRectMake (50, 50, 100, 100);
    2. [[Uicolor Redcolor] set];
    3. Uirectframe (rect);
    4. Cgcontextaddellipseinrect (context, rect);
    5. Cgcontextdrawpath (context, kcgpathfillstroke);

91.//Drawing Bezier Curves

92.//Setting the starting point

    1. Cgcontextmovetopoint (context, 20, 200);
    2. Cgcontextaddcurvetopoint (context, 100, 20, 200, 300, 300, 50);
    3. Cgcontextaddquadcurvetopoint (context, 150, 20, 300, 200);
    4. [[Uicolor Redcolor] set];
    5. Cgcontextdrawpath (context, kcgpathstroke);

98.//drawing a text

  1. NSString *str = @ "Hello world";
  2. CGRect rect = CGRectMake (50, 50, 200, 300);
  3. [[Uicolor Whitecolor] setfill];
  4. Uirectfill (rect);
  5. Uifont *font = [Uifont systemfontofsize:20];
  6. Uicolor *color = [Uicolor Redcolor];
  7. Create a paragraph style object that sets the style of the text to be displayed in a paragraph by setting its properties.
  8. Nsmutableparagraphstyle *style = [[Nsmutableparagraphstyle alloc] init];
  9. Style.linebreakmode = nslinebreakbywordwrapping;
  10. Style.alignment = Nstextalignmentcenter;
  11. [Str drawinrect:rect withfont:font linebreakmode:nslinebreakbywordwrapping Alignment:nstextalignmentcenter];
  12. [Str drawinrect:rect withattributes:
  13. @{
  14. Nsfontattributename:font,
  15. Nsforegroundcolorattributename:color,
  16. Nsparagraphstyleattributename:style
  17. }];
  18. This is a very lightweight way to draw text, and you don't need to create a Uilabel control, and you can tailor the effect to your needs.
  19. Drawing pictures
  20. 1. Use the method provided by Uikit to draw
  21. UIImage *image = [UIImage imagenamed:@ "mp3.jpg"];
  22. Picture Extrude appears in the specified rectangular area
  23. [Image drawinrect:cgrectmake (0, 0, 320, 200)];
  24. The picture tile appears in the specified area
  25. [Image drawaspatterninrect:cgrectmake (0, 0, 320, 200)];
  26. 2. Draw with the function provided by coregraphic
  27. The coordinate system of CG coordinate system transformed into Uikit
  28. Cgcontextsavegstate (context);
  29. (1) Clockwise rotation 180 degrees
  30. CGCONTEXTROTATECTM (context, M_PI);
  31. (2) x zoom to original-1 time times
  32. CGCONTEXTSCALECTM (Context,-1, 1);
  33. (3) Pan up
  34. CGCONTEXTTRANSLATECTM (context, 0,-image.size.height);
  35. Cgcontextdrawimage (context, CGRectMake (0, 0, Image.size.width, image.size.height), image. Cgimage);
  36. (4) Restore context
  37. Cgcontextrestoregstate (context);

Quartz 2D Simple Drawing

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.