Cat learning iOS (32) UI quartz2d matrix manipulation and picture clipping

Source: Internet
Author: User

Cat Share, must boutique

Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents

Matrix operations

Matrix operations: (rotate, scale, pan)
By manipulating the matrix, the drawing is deformed.

Rotation operation

Method: Cgcontextrotatectm (< #CGContextRef C#>, < #CGFloat angle#>) should accept two parameters (graphics context, radians)
Note: Setting the matrix operation must be done before adding the graphic, if it is finished after adding the graphic, it is not valid at this point.

Effect:

Code:

-(void) Drawrect:(CGRect) rect{//Draw quadrilateral    Gets the graphics context Cgcontextref ctx=uigraphicsgetcurrentcontext ();     //matrix operation     //Note: The set matrix operation must be preceded by the addition of the drawing information     //rotated 22.5 degrees     CGCONTEXTROTATECTM (CTX, M_pi_4*0.5 ) ;    //drawing  cgcontextaddrect (CTX, cgrectmake ( 150 , 100 , 100     , 100 ) ) ;    //render  cgcontextstrokepath (CTX) ;}  

View is able to display views because it has a layer on it, and future graphics are rendered to the layer.
Other words:
When rotated, the entire layer is rotated.


We can try more, draw two pictures, and then let him rotate, you can get his center where, such a small thing I will not nonsense.

Scaling

Method: Cgcontextscalectm (< #CGContextRef C#>, < #CGFloat Sx#>, < #CGFloat sy#>)
The method receives three parameters (graph context, scaling in x direction, scaling in the y direction)
Effect:

Code:

-(void) DrawRect:(CGRect) rect{//Get Graphics Context Cgcontextref ctx=uigraphicsgetcurrentcontext ();    //matrix operations    //NOTE: The set matrix operation must be preceded by the addition of the drawing information.    //scaling, X-direction scaling 0.4 times times, y-direction scaling twice times    CGCONTEXTSCALECTM (CTX, 0.4, 2.0);//Drawing    //Draw quadrilateral    Cgcontextaddrect (CTX, cgrectmake ()   ;//Draw a circle    Cgcontextaddellipseinrect (CTX, cgrectmake (   +, +)) ;//Rendering    Cgcontextstrokepath (CTX);}

According to the code, we draw a circle and a square, but it becomes a graph effect after zooming.

Translation

Method: Cgcontexttranslatectm (< #CGContextRef C#>, < #CGFloat Tx#>, < #CGFloat ty#>)
The method receives three parameters (graphics context, offset in x direction, offset in y direction)

Effect:

-(void) DrawRect:(CGRect) rect{//Get Graphics Context Cgcontextref ctx=uigraphicsgetcurrentcontext ();    //matrix operations    //NOTE: The set matrix operation must be preceded by the addition of the drawing information.    //pan, move 10,y direction in x direction    CGCONTEXTTRANSLATECTM (CTX, ten, max);//Drawing    //Draw quadrilateral    Cgcontextaddrect (CTX, cgrectmake ()    ;//Draw a circle    Cgcontextaddellipseinrect (CTX, cgrectmake (    +, +));//Rendering    Cgcontextstrokepath (CTX);}
Picture clipping

First and foremost, our favorite cat.

I'm going to make him round, the code says, OK.

- (void)drawRect:(CGRect)rect{    //画圆,以便以后指定可以显示图片的范围    //获取图形上下文    CGContextRef ctx=UIGraphicsGetCurrentContext();    CGContextAddEllipseInRect(ctx, CGRectMake(100100100100));    //指定上下文中可以显示内容的范围就是圆的范围    CGContextClip(ctx);    UIImage *image2=[UIImage imageNamed:@"cat"];    [image2 drawAtPoint:CGPointMake(100100)];}

Add watermark to the picture does not say, in fact, is to write text to write pictures, open source framework is also a heap of piles, cats are not showing off.

Cat learns iOS (32) UI quartz2d matrix operations and picture clipping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.