ios– drawing describes three ways to draw an image one. Subclass UIView, in DrawRect: Method drawing
When you execute a method, the system creates the canvas itself (cgcontext), and the canvas is pushed to the top of the stack
After execution, the system executes the pop out of the canvas.
- (void)drawRect:(CGRect)rect{ CGContextRef con = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100)); CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor); CGContextFillPath(con);}
Two. Direct Uigraphicsbeginimagecontextwithoptions drawing
Uigraphicsbeginimagecontextwithoptions (
Cgsize size,
BOOL Opaque,
CGFloat scale
);
Parameter: size– Canvas size
Parameter: opaque– opaque
Parameters: scale– magnification, set to 0, representation and screen adaptive. (This parameter is the same as the scale in image)
Uigraphicsbeginimagecontextwithoptions (Cgsizemake ( -, -),NO,0); uibezierpath* p = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0,0, -, -)]; [[UicolorBluecolor] Setfill]; [P fill];UIImage* im = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext ();Uiimageview*imgview = [[UiimageviewAlloc]initwithimage:im]; Imgview. Frame= CGRectMake ( -, -, IM. Size. Width, IM. Size. Height); [ Self. ViewAddsubview:imgview];
Three. Layer.delegate agent to draw pictures
UIView Drawlayer:incontext: Implements a drawing task in a method. Drawlayer:incontext: Method is a proxy method that paints the contents of a layer. To be able to invoke the Drawlayer:incontext: method, we need to set the proxy object for the layer.
Note the point:
1. Drawlayer:incontext: The proxy method needs to be written in the UIView subclass, and the memory over-release issue occurs if written in Uiviewcontroller.
2. The UIView object should not be set as the delegate object for the display layer, because the UIView object is already a proxy object for the implicit layer, and setting it to another layer's delegate object will cause problems.
Here is the declaration and implementation file of the implementation code
@interface LayerDelegate : NSObject@end@interface LayerDelegateView : UIView@end
@implementation layerdelegate - (void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) ctx{//1. Drawing a drawing //Draw a circleCgcontextaddellipseinrect (CTX, CGRectMake ( -, -, -, -));//Set properties (color)Cgcontextsetrgbfillcolor (CTX,0,0,1,1);//2. RenderingCgcontextfillpath (CTX);}@end @interface layerdelegateview ()@property(Strong,nonatomic) Layerdelegate *delegate;@end @implementation layerdelegateview - (void) DrawRect: (CGRect) Rect {//Drawing code}-(Instancetype) initWithFrame: (CGRect) frame{ Self= [SuperInitwithframe:frame];if( Self{Calayer *mylayer = [Calayer layer]; Self. Delegate= [[Layerdelegate alloc]init]; Mylayer. Delegate= Self. Delegate; Mylayer. BackgroundColor= [UicolorBrowncolor]. Cgcolor; Mylayer. Bounds= CGRectMake (0,0, $, Max); Mylayer. Anchorpoint= Cgpointzero; Mylayer. Position= Cgpointmake ( -, -); Mylayer. Cornerradius= -; Mylayer. Shadowcolor= [UicolorBlackcolor]. Cgcolor; Mylayer. Shadowoffset= Cgsizemake (Ten, -); Mylayer. Shadowopacity=0.6; [Mylayer Setneedsdisplay];//Call this method, Drawlayer:incontext: Method will be called. [ Self. LayerAddsublayer:mylayer]; }return Self;}@end
Introduction to Drawing Cgcontext stacks
Cgcontext is stored in a drawing context stack area of the system, and when we edit cgcontext, we can push the cgcontext to the top of the stack and then Gcontext pop out of the stack when the edits are complete. The following code provides a brief description of its use.
UIImage*IMG1;UIImage*img2;//Drawing in drawing context 1Uigraphicsbeginimagecontextwithoptions (Cgsizemake ( $, -),NO,0.0); [@"The first one"Drawinrect:cgrectmake (Ten, -, the, -) withattributes:@{nsfontattributename:[UifontSystemfontofsize: A]}]; IMG1 = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicspushcontext (Uigraphicsgetcurrentcontext ()); Uigraphicsendimagecontext ();//!!!: This and so on, try to erase it. //Jump to draw in drawing context 2Uigraphicsbeginimagecontextwithoptions (Cgsizemake ( $, -),NO,0.0); [@"The second One"Drawinrect:cgrectmake (Ten, -, the, -) withattributes:@{nsfontattributename:[UifontSystemfontofsize: A]}]; Img2 = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext ();//pop back to drawing in context 1Uigraphicspopcontext (); [@"The first one"Drawinrect:cgrectmake ( the, -, the, -) withattributes:@{nsfontattributename:[UifontSystemfontofsize: A]}]; IMG1 = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicspushcontext (Uigraphicsgetcurrentcontext ()); Uigraphicsendimagecontext ();//pop back to drawing in context 1Uigraphicspopcontext (); [@"The first one"Drawinrect:cgrectmake (Ten, +, the, -) withattributes:@{nsfontattributename:[UifontSystemfontofsize: A]}]; IMG1 = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext ();//Drawing context 1 and picture in painting context 2 are displayed Uiimageview*imgview1 = [[UiimageviewALLOC]INITWITHIMAGE:IMG1]; ImgView1. Frame= CGRectMake (0, -, $, -); ImgView1. BackgroundColor= [UicolorYellowcolor]; [ Self. ViewADDSUBVIEW:IMGVIEW1];Uiimageview*IMGVIEW2 = [[UiimageviewALLOC]INITWITHIMAGE:IMG2]; ImgView2. Frame= CGRectMake (0, -, $, -); ImgView2. BackgroundColor= [UicolorRedcolor]; [ Self. ViewADDSUBVIEW:IMGVIEW2];
Reference articles
iOS Drawing Tutorials
iOS Drawing Practical Examples Explained
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ios--Drawing Introduction