iOS image processing (1) Drawing simple geometry

Source: Internet
Author: User

Drawing an image through coregraphic generally has the following steps:

1: Get the Graphics context object

Drawing an image first requires a graphical context object (CGCONTEXTREF) that maintains a cgcontextref stack in the system before the DrawRect method call of the UI control. A graphical context object is created for the current drawing environment and is placed at the top of the Cgcontextref stack, where the image context object can be obtained through Uigraphicsgetcurrentcontext ().

2: Set the painting properties, you can configure the line color, fill color, line width, shadow, segment endpoint shape, point line mode, line connection point style, overlay mode and other properties

3: Drawing images from API

- (void) DrawRect: (cgrect) rect{//Get Image Context ObjectCgcontextref context =Uigraphicsgetcurrentcontext (); //Set Segment WidthCgcontextsetlinewidth (Context,2); //sets the line color through the RGB mode, and the last parameter represents transparencyCgcontextsetrgbstrokecolor (Context, -/255.0, -/255.0, -/255.0,1); //Set Fill ColorCgcontextsetfillcolorwithcolor (context, [Uicolor Greencolor].    Cgcolor); //Set the shadow, the second parameter is the offset of the shadow on the x-axis and the y-axis, the third parameter sets the blur degree of the shadow, 0 is not blurred, the shadow default color is black, the transparency 0.3Cgcontextsetshadow (Context, Cgsizemake (3,3), 5); //sets the segment endpoint shape, the second parameter is an enumeration type, and contains 3 optionsCgcontextsetlinecap (context, kcglinecapround); //Set line connection stylecgcontextsetlinejoin (context, kcglinejoinround); //Draw line segments, points is an array of points, two points to determine a line, the following arrays have 8 points, you can determine 4 line segments    ConstCgpoint points[] = {Cgpointmake (Ten, -), Cgpointmake ( -, -), Cgpointmake ( -, -), Cgpointmake ( -, -), Cgpointmake ( -, -), Cgpointmake (Ten, -), Cgpointmake (Ten, -), Cgpointmake (Ten, -)}; //DrawingCgcontextstrokelinesegments (context, points,8); //Fill RectangleCgcontextfillrect (Context, CGRectMake ( -, -, the, -)); //Draw a rectangleCgcontextstrokerect (Context, CGRectMake ( -, -, the, -)); //Draw EllipseCgcontextstrokeellipseinrect (Context, CGRectMake ( -, -, the, -)); //Draw Dashed Lines//dotted line mode is required to draw dashed lines.CGFloat pattern[] = {5,5}; Cgcontextsetlinedash (context, 1, Pattern,2); ConstCgpoint points2[] = {Cgpointmake ( -, the), Cgpointmake ( -, the)}; Cgcontextstrokelinesegments (context, POINTS2,2);}

Run results

In the above code, the Cgcontextsetlinedash function is used to set the point line pattern, the function has 4 parameters, the first parameter represents the image context object, the second parameter is used with the third parameter to specify the length of the first point line, and the third parameter is a floating point array. The last parameter is the length of the array in the third argument.

The array we passed is {5,5} represents the dot line length 5, Interval 5, if the array is {2,3,4} represents the point line length 2, Interval 3, point line length 4, interval 2, Dot line length 3, Interval 4 this goes on.

The first digit of the array minus the 2nd parameter of the function represents the length of the first point line, such as the first number of the array in the previous example is 5, the 2nd number of the parameter is 1, so the first point line has a length of 4.

iOS image processing (1) Drawing simple geometry

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.