Before drawing an image, you first need to get a graphical context object (CGCONTEXTREF), and a cgcontextref stack is maintained 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 (). We can also create our own image context objects, Quartz 2d provides the appropriate APIs for developers to create various contextual objects, and when we use Uikit to draw in iOS memory, creating context is easier, Simply call Uigraphicsbeginimagecontextwithoptions to create a context that is based on a memory drawing
- (void) DrawRect: (cgrect) rect{//creates a bitmap-based context, and sets it as the current context//Drawing Area Size | transparency | Zoom factor//uigraphicsbeginimagecontextwithoptions (Rect.size, YES, 1); //transparency Default Yes scaling Factor 1.0Uigraphicsbeginimagecontext (rect.size); //Get Image Context ObjectCgcontextref context =Uigraphicsgetcurrentcontext (); Cgcontextsetfillcolorwithcolor (context, [Uicolor Magentacolor]. Cgcolor); Cgcontextsetstrokecolorwithcolor (context, [Uicolor Greencolor]. Cgcolor); //Draw EllipseCgcontextstrokerect (Context, CGRectMake ( -, -, -, -)); //Draw a rectangleCgcontextfillellipseinrect (Context, CGRectMake ( Max, -, -, -)); //Draw Text[@"This is a Jok"Drawatpoint:cgpointmake ( -, Max) Withattributes:@{nsfontattributename:[uifont Fontwithname:@"Arial"Size -],nsforegroundcolorattributename:[uicolor Redcolor]}]; //get the picture in the context of a drawingUIImage *image =Uigraphicsgetimagefromcurrentimagecontext (); //End DrawingUigraphicsendimagecontext (); //display a picture on the interface[Image Drawinrect:rect]; //picture is stored in a sandboxNSString *path = [[Nshomedirectory () stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"Pic.png"]; NSLog (@"%@", path); [Uiimagepngrepresentation (image) Writetofile:path atomically:yes];}
As we can see from the example above, the steps of drawing in memory are exactly the same as the steps of drawing on the view, which can be displayed to the view after drawing the picture in memory, or stored in the application sandbox, using the context of the memory drawing, we can easily implement the function of the drawing board
@implementationZltview {cgpoint _firstpoint; Cgpoint _lastpoint; Cgcontextref _imagecontext; UIImage*_image;}- (ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {uigraphicsbeginimagecontext (self.frame.size); _imagecontext=Uigraphicsgetcurrentcontext (); Cgcontextsetstrokecolorwithcolor (_imagecontext, [Uicolor Purplecolor]. Cgcolor); Cgcontextsetlinewidth (_imagecontext,5); } returnSelf ;}- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{Uitouch*touch =[touches anyobject]; Cgpoint TouchPoint=[Touch locationinview:self]; _firstpoint=TouchPoint;}- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{Uitouch*touch =[touches anyobject]; Cgpoint TouchPoint=[Touch locationinview:self]; _lastpoint=TouchPoint; Cgcontextmovetopoint (_imagecontext, _firstpoint.x, _FIRSTPOINT.Y); Cgcontextaddlinetopoint (_imagecontext, _lastpoint.x, _LASTPOINT.Y); Cgcontextdrawpath (_imagecontext, Kcgpathstroke); _image=Uigraphicsgetimagefromcurrentimagecontext (); [Self setneedsdisplay]; _firstpoint=_lastpoint;}- (void) DrawRect: (cgrect) rect{[_image drawinrect:rect];}
iOS image processing (6) Drawing in memory context