Reference: http://www.mgenware.com/blog/?p=493
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 DrawingUigraphicsbeginimagecontext (self.view.bounds.size); //Get current CgcontextrefCgcontextref GC =Uigraphicsgetcurrentcontext (); //Create a transform that is used to transfer coordinates so that we do not calculate the coordinates according to the actual displayCgaffinetransform transform = cgaffinetransformmaketranslation ( -, -); //Create CgmutablepathrefCgmutablepathref Path =cgpathcreatemutable (); //left EyeCgpathaddellipseinrect (Path, &transform, CGRectMake (0,0, -, -)); //Right EyeCgpathaddellipseinrect (Path, &transform, CGRectMake ( the,0, -, -)); //LaughCgpathmovetopoint (Path, &transform, -, -); Cgpathaddarc (Path,&transform, -, -, -,0, M_PI, NO); //add Cgmutablepathref to the current contextCgcontextaddpath (GC, PATH); //Set drawing Properties[[Uicolor Bluecolor] setstroke]; Cgcontextsetlinewidth (GC,2); //Perform paintingCgcontextstrokepath (GC); //get the image from the context and display it on the interfaceUIImage *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 DrawingUigraphicsbeginimagecontext (self.view.bounds.size); //Get current CgcontextrefCgcontextref 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 displayCGCONTEXTTRANSLATECTM (GC, -, -); //left EyeCgcontextaddellipseinrect (GC, CGRectMake (0,0, -, -)); //Right EyeCgcontextaddellipseinrect (GC, CGRectMake ( the,0, -, -)); //LaughCgcontextmovetopoint (GC, -, -); Cgcontextaddarc (GC, -, -, -,0, M_PI, NO); //Set drawing Properties[[Uicolor Bluecolor] setstroke]; Cgcontextsetlinewidth (GC,2); //Perform paintingCgcontextstrokepath (GC); //get the image from the context and display it on the interfaceUIImage *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 DrawingUigraphicsbeginimagecontext (self.view.bounds.size); //Create UibezierpathUibezierpath *path =[Uibezierpath Bezierpath]; //left Eye[Path Appendpath:[uibezierpath Bezierpathwithovalinrect:cgrectmake (0,0, -, -)]]; //Right Eye[Path Appendpath:[uibezierpath Bezierpathwithovalinrect:cgrectmake ( the,0, -, -)]]; //Laugh[Path Movetopoint:cgpointmake ( -, -)]; //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: -StartAngle:0Endangle: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 ( -, -)]; //Set drawing Properties[[Uicolor Bluecolor] setstroke]; [Path Setlinewidth:2]; //Perform painting[path stroke]; //get the image from the context and display it on the interfaceUIImage *img =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); Uiimageview*imgview =[[Uiimageview alloc] initwithimage:img]; [Self.view Addsubview:imgview];}
[Ios]ios draw line uigraphicsbeginimagecontextwithoptions--generate transparent graphics