Drawing in iOS is based on points to determine location, here is the concept of a coordinate system
In the core graphics, the coordinate origin of the coordinate system is in the lower-left corner of the screen, the positive direction of the y-axis is upward, while in the uikit the coordinate origin is in the upper-left corner of the screen, along the y-axis positive direction.
We can convert any 2-dimensional coordinate system through a 3-row, 3-Column transformation matrix (or through a simpler API), and commonly used transformations include moving (translate), scaling (scale), and rotation (rotate).
1 mobile
-(void) DrawRect: (cgrect) rect{ // Get Image Contextual Object cgcontextref context = Uigraphicsgetcurrentcontext (); - - ); *image = [UIImage imagenamed:@ "mario.jpg"]; [Image Drawinrect:rect];}
2 Scaling
-(void) DrawRect: (cgrect) rect{ // Get Image Contextual Object cgcontextref context = Uigraphicsgetcurrentcontext (); // x-axis and y-axis are 0.5 times times the previous 0.5 0.5 ); *image = [UIImage imagenamed:@ "mario.jpg"]; [Image Drawinrect:rect];}
3 rotation
-(void) DrawRect: (cgrect) rect{ // Get Image Contextual Object cgcontextref context = Uigraphicsgetcurrentcontext (); // rotates 30 degrees (relative to Origin (0,0)) - the ); *image = [UIImage imagenamed:@ "mario.jpg"]; [Image Drawinrect:rect];}
Frequent coordinate changes often cause developers to be confused about the state of the current coordinates and the location of the origin of the coordinates, which can be used when
Cgcontextsavegstate (Cgcontextref) and Cgcontextrestoregstate (CGCONTEXTREF) Two functions store the current drawing environment (including coordinate systems, drawing properties) and restore the last saved drawing environment
Coordinate multiple changes operation
- (void) DrawRect: (cgrect) rect{//Get Image Context ObjectCgcontextref context =Uigraphicsgetcurrentcontext (); UIImage*image = [UIImage imagenamed:@"girl.jpg"]; Cgcontextsavegstate (context); CGCONTEXTSCALECTM (Context,0.5,0.5); [Image Drawinrect:rect]; Cgcontextrestoregstate (context); Cgcontextsavegstate (context); CGCONTEXTTRANSLATECTM (Context,rect.size.width/2,-rect.size.height/2); [Image Drawinrect:rect]; Cgcontextrestoregstate (context);}
Coordinates are rotated at the origin, and the image is rotated around the center by panning the origin to the center of the screen.
- (void) DrawRect: (cgrect) rect{//Get Image Context ObjectCgcontextref context =Uigraphicsgetcurrentcontext (); UIImage*image = [UIImage imagenamed:@"girl.jpg"]; CGCONTEXTTRANSLATECTM (Context,rect.size.width/2, rect.size.height/2); CGCONTEXTROTATECTM (Context,- the* M_PI/ the); [Image Drawinrect:cgrectmake (0-rect.size.width/2,0-rect.size.height/2, Rect.size.width, Rect.size.height)];}
In addition to using the above three coordinate transformation methods, you can also use CGCONTEXTCONCATCTM (cgcontextref,cgaffinetransform) for coordinate transformation, which requires the creation of Cgaffinetransform, which represents a 3 of the transformation matrix, you can achieve 2-dimensional coordinates of all transformations.
You can refer to this blog for the principles and usage of cgaffinetransform.
iOS image processing (4) Coordinate changes