IOS: Use Cgcontextref,cgpath and Uibezierpath to paint

Source: Internet
Author: User

These three kinds of things: CGContextRef , CGPath and UIBezierPath . are essentially the same, all using quartz to paint. Just expose the drawing operations to different API levels, and there are, of course, some small differences in the implementation.

We will mainly use these 3 types, draw out the same picture, like Next, a smiley face:

First use the quartz CGPath to do this picture. Very simply, first create the transform that is used to transfer the coordinates, and then create a CGMutablePathRef (belonging to the CGPath type) object. Then through two CGPathAddEllipseInRect and a CGPathAddArc function to draw the path of two eyes and a mouth, pay attention to CGAffineTransform the address of the pass in, so that transform will be applied. Then CGPath add this to the current CGContextRef , and finally by CGContextRef performing the painting.

Code:

-(void) viewdidload
{
[Super Viewdidload];

Start Image Drawing
Uigraphicsbeginimagecontext (self.view.bounds.size);
Get current Cgcontextref
Cgcontextref GC = Uigraphicsgetcurrentcontext ();

Create a transform that is used to transfer coordinates so that we do not calculate the coordinates according to the actual display
Cgaffinetransform transform = cgaffinetransformmaketranslation (50, 50);
Create Cgmutablepathref
Cgmutablepathref path = cgpathcreatemutable ();
Left eye
Cgpathaddellipseinrect (Path, &transform, CGRectMake (0, 0, 20, 20));
Right eye
Cgpathaddellipseinrect (Path, &transform, CGRectMake (80, 0, 20, 20));
Laugh
Cgpathmovetopoint (Path, &transform, 100, 50);
Cgpathaddarc (path, &transform, 0, M_PI, NO);
Add Cgmutablepathref to the current context
Cgcontextaddpath (GC, PATH);
Set drawing Properties
[[Uicolor Bluecolor] setstroke];
Cgcontextsetlinewidth (GC, 2);
Perform painting
Cgcontextstrokepath (GC);

Get the image from the context and display it on the interface
UIImage *img = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();

Uiimageview *imgview = [[Uiimageview alloc] initwithimage:img];
[Self.view Addsubview:imgview];
}

Next, instead of using the CGPath related functions of the type, we use the relevant functions completely CGContextRef , and these functions are actually exactly equivalent to what is said above CGPath .

It is important to note that for full use CGContextRef , the transform application needs to CGContextTranslateCTM use functions.

Full code:

-(void) viewdidload
{
[Super Viewdidload];

Start Image Drawing
Uigraphicsbeginimagecontext (self.view.bounds.size);
Get current Cgcontextref
Cgcontextref GC = Uigraphicsgetcurrentcontext ();

Use the CGCONTEXTTRANSLATECTM function to transfer the coordinates of the transform so that we do not calculate the coordinates according to the actual display
CGCONTEXTTRANSLATECTM (GC, 50, 50);
Left eye
Cgcontextaddellipseinrect (GC, CGRectMake (0, 0, 20, 20));
Right eye
Cgcontextaddellipseinrect (GC, CGRectMake (80, 0, 20, 20));
Laugh
Cgcontextmovetopoint (GC, 100, 50);
Cgcontextaddarc (GC, 0, M_PI, NO);
Set drawing Properties
[[Uicolor Bluecolor] setstroke];
Cgcontextsetlinewidth (GC, 2);
Perform painting
Cgcontextstrokepath (GC);

Get the image from the context and display it on the interface
UIImage *img = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
Uiimageview *imgview = [[Uiimageview alloc] initwithimage:img];
[Self.view Addsubview:imgview];
}

The graph above is also plotted.

Finally we use the UIBezierPath type to complete the above figure, UIBezierPath it is interesting, it wraps the Quartz related API, itself exists UIKit in, therefore is not based on the C API, but is based on the Objective-c object. Then a very important point is due to leave the quartz drawing, so do not need to consider the problem of the y-axis rollover, in the arc, the clockwise parameters are the same as the reality, if the need to pass clockwise YES , rather than as the quartz environment transmitted NO .

Next, the ellipse is created using the bezierPathWithOvalInRect method, where Oral the name is not quartz Ellipse .

The last UIBezierPath -noticed applyTransform method needs to be called last.

Full code:

-(void) viewdidload
{
[Super Viewdidload];

Start Image Drawing
Uigraphicsbeginimagecontext (self.view.bounds.size);

Create Uibezierpath
Uibezierpath *path = [Uibezierpath Bezierpath];
Left eye
[Path Appendpath:[uibezierpath bezierpathwithovalinrect:cgrectmake (0, 0, 20, 20)];
Right eye
[Path Appendpath:[uibezierpath bezierpathwithovalinrect:cgrectmake (80, 0, 20, 20)];
Laugh
[Path Movetopoint:cgpointmake (100, 50)];
Note here that the clockwise parameter is yes and not no, because there is no quartz and there is no need to consider the problem of y-axis flipping
[Path Addarcwithcenter:cgpointmake (radius:50) startangle:0 endangle:m_pi Clockwise:yes];
Use the Applytransform function to transfer the coordinates of the transform so that we do not calculate the coordinates according to the actual display
[Path Applytransform:cgaffinetransformmaketranslation (50, 50)];
Set drawing Properties
[[Uicolor Bluecolor] setstroke];
[Path Setlinewidth:2];
Perform painting
[Path stroke];

Get the image from the context and display it on the interface
UIImage *img = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
Uiimageview *imgview = [[Uiimageview alloc] initwithimage:img];
[Self.view Addsubview:imgview];

IOS: Use Cgcontextref,cgpath and Uibezierpath to paint

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.