Quartz 2D is a two-dimensional graphics drawing engine that supports the iOS environment and Mac OS x environment, official documentation: Quartz 2D Programming Guide.
One, coordinate system
Such a coordinate system leads us to use the quartz 2D picture is inverted, we have to do the following to get the picture effect we want:
1. Shift the height of the canvas extension y-axis
2. Flip the y-axis vertically
The code for these 2 steps is:
1 0 , height); 2 1.0,-1.0);
The Test code is:
//Code 1- (void) DrawRect: (cgrect) Rect {cgcontextref context=Uigraphicsgetcurrentcontext (); Cgcontextsetrgbfillcolor (Context,1,0,0,1); Cgcontextfillrect (Context, CGRectMake (0, -, -, -)); NSString*text=@"text"; Uifont*font=[uifont systemfontofsize: -]; [Text Drawatpoint:cgpointmake (0, $) withAttributes:font.fontDescriptor.fontAttributes]; UIImage*img=[uiimage imagenamed:@"gg.jpg"]; [img Drawinrect:cgrectmake (0, -, -, -)];}
//Code 2-(ID) initWithFrame: (cgrect) frame{if(self=[Super Initwithframe:frame]) {[self setbackgroundcolor:[uicolor redcolor]]; Uiimageview*imgview=[[Uiimageview alloc] initWithFrame:self.bounds]; Cgcolorspaceref ColorSpace=Cgcolorspacecreatedevicergb (); floatWidth=Self.bounds.size.width; floatheight=Self.bounds.size.height; //256=10000000 intBitspercomponent=8; //Rgba*8*width intbytesperrow=4*8*width; Cgcontextref Context=cgbitmapcontextcreate (NULL, width, height, bitspercomponent, Bytesperrow, ColorSpace, Kcgimagealphapremultipliedlast|Kcgbitmapbyteorderdefault); //Flip CanvasCGCONTEXTTRANSLATECTM (Context,0, height); CGCONTEXTSCALECTM (Context,1.0, -1.0); Uigraphicspushcontext (context); //Canvas Transparent//cgcontextfillrect (context, self.bounds);Cgcontextsetrgbfillcolor (Context,1,0,0,1); Cgcontextsetrgbstrokecolor (Context,0,1,0,1); Cgcontextfillrect (Context, CGRectMake (0, -, -, -)); NSString*text=@"text"; Uifont*font=[uifont systemfontofsize: -]; [Text Drawatpoint:cgpointmake (0, $) withAttributes:font.fontDescriptor.fontAttributes]; UIImage*img=[uiimage imagenamed:@"gg.jpg"]; [img Drawinrect:cgrectmake (0, -, -, -)]; Cgimageref cgimg=cgbitmapcontextcreateimage (context); UIImage*resultimg =[UIImage imagewithcgimage:cgimg]; Cgcontextrelease (context); Cgcolorspacerelease (ColorSpace); Cgimagerelease (CGIMG); Imgview.image=resultimg; [Self addsubview:imgview]; } returnSelf ;}
The 2-segment code implements the same effect, code 1 does not do the flip operation, because the Uikit in the Uigraphicsgetcurrentcontext () has helped us to adapt to the Uikit coordinate system.
Reprint Please specify source: http://www.cnblogs.com/bandy/p/4341538.html
Quartz 2D use in iOS overview one: coordinate system