(1) The drawing context is the target object of a drawing. It defines the basic attributes of the drawing, such as color, drawing range, line width, and style. (2) A context is created through uiview. You can use statements similar to the following to obtain the current context. cgcontextref currentcontext = uigraphicsgetcurrentcontext (); (3) if you want to save the current state before modifying it, you can use uigraphicspushcontext; to restore the saved context, you can use uigraphicspopcontext. (4) path: the path is actually a curve or line marked by a series of coordinate points. After being created, you can draw a line along it or fill the closed space. // Override the initialization method. Create and set the custom context During view initialization.
-(Instancetype) initwithframe :( cgrect) frame {
If (Self = [Super initwithframe: frame]) {
Self. backgroundcolor = [uicolor whitecolor];
Cgcolorspaceref colorspace = cgcolorspacecreatedevicergb (); // specify the color space as RGB whiteboardcontext = cgbitmapcontextcreate (null, self. frame. size. width, self. frame. size. height, 8, 4 * self. frame. size. width, colorspace, kcgimagealphapremultipliedfirst);} return self;} Handle this warning: http://blog.csdn.net/thanklife/article/details/25790433 //
// Whiteboardview. m
// Draw2
//
// Created by K-net on 15-5-7.
// Copyright (c) 2015 K-Net. All rights reserved.
//
# Import "whiteboardview. H"
// # If _ iphone_ OS _version_max_allowed> _ iphone_6_1
// # Define kcgimagealphapremultipliedlast (kcgbitmapbyteorderdefault | kcgimagealphapremultipliedlast)
// # Else
// # Define kcgimagealphapremultipliedlast
// # Endif
@ Implementation whiteboardview
// Override the initialization method. Create and set the custom context During view initialization.
-(Instancetype) initwithframe :( cgrect) frame {
If (Self = [Super initwithframe: frame]) {
Self. backgroundcolor = [uicolor whitecolor];
Cgcolorspaceref colorspace = cgcolorspacecreatedevicergb (); // specify the color space as RGB.
Whiteboardcontext = cgbitmapcontextcreate (null, self. frame. size. width, self. frame. size. height, 8, 4 * self. frame. size. width, colorspace, (cgbitmapinfo) kcgimagealphapremultipliedfirst );
// Kcgbitmapalphainfomask = 0x1f,
// Kcgbitmapfloatcomponents = (1 <8 ),
// (Cgbitmapinfo) kcgimagealphapremultipliedfirst
// Kcgbitmapbyteordermask = 0x7000,
// Kcgbitmapbyteorderdefault = (0 <12 ),
// Kcgbitmapbyteorder16little = (1 <12 ),
// Kcgbitmapbyteorder32little = (2 <12 ),
// Kcgbitmapbyteorder16big = (3 <12 ),
// Kcgbitmapbyteorder32big = (4 <12)
Cgcolorspacerelstrap (colorspace );
// Create a new cglayer for drawing
Whiteboardlayer = cglayercreatewithcontext (whiteboardcontext, self. Frame. Size, null );
// Obtain the context of the new layer
Cgcontextref layercontext = cglayergetcontext (whiteboardlayer );
// Specify the width of the new layer Context
Cgcontextsetlinewidth (layercontext, 1.5 );
// Specify the line header to be circular.
// Enum cglinecap {
// Kcglinecapbutt,
// Kcglinecapround,
// Kcglinecapsquare
//};
Cgcontextsetlinecap (layercontext, kcglinecapround );
// Specify the line color, black
Cgcontextsetrgbstrokecolor (layercontext, 0.0, 0.7, 0.0, 1 );
}
Return self;
}
// Override the drawrect Method
-(Void) drawrect :( cgrect) rect {
// Obtain the current context first
Cgcontextref currentcontext = uigraphicsgetcurrentcontext ();
// Generate Bitmap Based on context content
Cgimageref image = cgbitmapcontextcreateimage (currentcontext );
// Draw bitmap to context
Cgcontextdrawimage (currentcontext, [self bounds], image );
// Draw the whiteboardlayer to the current context.
Cgcontextdrawlayerinrect (currentcontext, [self bounds], whiteboardlayer );
}
// Screen touch event touch start
-(Void) touchesbegan :( nsset *) touches withevent :( uievent *) event {
// Get the touch object
Uitouch * thetouch = [touches anyobject];
If ([thetouch tapcount] = 2) {// if you double-click it, the image is cleared.
// Cgcontextclearrect: Specifies the image drawn in the rectangle area.
Cgcontextclearrect (whiteboardcontext, [self bounds]); // clear
[Self setneedsdisplay]; // refresh the screen
} Else {
[Self touchesmoved: touches withevent: event];
}
}
// Triggered by finger movement
-(Void) touchesmoved :( nsset *) touches withevent :( uievent *) event {
Uitouch * thetouch = [touches anyobject]; // obtain any finger touch
// Obtain the current location
Cgpoint currenttouchlocation = [thetouch locationinview: Self];
// Obtain the last location
Cgpoint lasttouchlocation = [thetouch previuslocationinview: Self];
// Obtain the context
Cgcontextref layercontext = cglayergetcontext (whiteboardlayer );
// Start to define path
Cgcontextbeginpath (layercontext );
// Move the start point of path to the previous position
Cgcontextmovetopoint (layercontext, lasttouchlocation. X, lasttouchlocation. y );
// Link between the last position and the current position, which is included in the path
Cgcontextaddlinetopoint (layercontext, currenttouchlocation. X, currenttouchlocation. y );
// Draw lines along path
Cgcontextstrokepath (layercontext );
[Self setneedsdisplay]; // refresh the screen
Nslog (@ "-==================== ");
}
@ End
Quartz 2D drawing