Unable to understand the transformation of the iOS core coordinate system, need help

I’m trying to understand the coordinate system of 2d quartz, I am currently reading the Apple reference book and the old book “Quartz 2d graphics for mac os x developer”.

I understand the concept of the concept of "space-space" and "device-space", that the device space can have different coordinates by default and the coordinates of the space-device can not be changed, and we display the user space by changing its coordinate system, connecting to the device- space to achieve the desired result.


  • First part of the problem

Quartz 2d graphics for Mac OS X Developer book say:

When transforming the coordinate system , you must have another one to compare it with. The transformation provides a relative mapping from one coordinate system to another. When you draw in a transformed coordinate system, the transformation displays a return to the first coordinate system . The appearance of this On the graph in a fixed coordinate system, the transformation affects.

I did not get this highlighted in bold.

and

Quartz 2D Programming Guide says:

Quartz performs device independence with a separate coordinate system-user space — mapping it to the output coordinate system of the device-device space — using the current transformation matrix or CTM. a matrix is ​​a mathematical construct used to effectively describe a set of related equations. The current transformation matrix is ​​a specific type of matrix called an affine transformation that maps points from one coordinate space to another using translation, rotation, and scaling (calculations that move, rotate, and change the size of the coordinate system).

The current transformation matrix has a secondary purpose: it allows you to convert objects to objects. For example, to draw a box rotated 45 degrees, you rotate the page coordinate system (CTM) before you draw a window. Quartz accesses the output device using a rotating coordinate system.

The confusion is that "quartz refers to the output device using a rotated coordinate system." If I want one object (image, etc.) to rotate, and the other without rotation, then what will happen? We have whole coordinates rotating, will each figure be rotated?

I try different experiments, but I can’t wrap this topic around myself, create an image that draws two lines, replicating the lower left coordinate system in Photoshop, and then added to my project to see how the coordinates behave, calling CGContextRotateCTM(myContext, 45); in the drawrect method, but it did nothing for the image that I included in the xib file using an interface constructor that places the image inside uiimage.

this code is from the quartz two-dimensional programming guide

 CGContextRef myContext = UIGraphicsGetCurrentContext(); CGRect contextRect = self.bounds; CGContextTranslateCTM(myContext, 0, contextRect.size.height); CGContextRotateCTM(myContext, 45); //my modification CGContextScaleCTM(myContext, 1, -1); float w, h; w = contextRect.size.width; h = contextRect.size.height; CGAffineTransform myTextTransform; CGContextSelectFont (myContext, "Helvetica-Bold", h/10, kCGEncodingMacRoman); CGContextSetCharacterSpacing (myContext, 10); CGContextSetTextDrawingMode (myContext, kCGTextFillStroke); CGContextSetRGBFillColor (myContext, 0, 1, 0, .5); CGContextSetRGBStrokeColor (myContext, 0, 0, 1, 1); myTextTransform = CGAffineTransformMakeRotation(0); CGContextSetTextMatrix (myContext, myTextTransform); CGContextShowTextAtPoint (myContext, 0, 50, "Quartz 2D", 9); 

but does this code change the text drawn on the screen, and not the image that I added?


  • Second part of the problem

This is also from the 2D quartz programming guide:

... apply a transformation that translates the beginning to the upper left corner of the PDF context and scales the y-coordinate by -1.

Using a scaling transform to negate the y coordinate changes some conventions in the quartz drawing. For example, if you call CGContextDrawImage to draw an image in context, the image is modified by the transformation when it is pulled to the destination ....

I already do this -1 thing, but I don’t affect the image, and this -1 thing is still not clear to me.

I have read this document several times, tried google search, but not a useful tutorial, and also no books are available, only books written in 2004, 2005, 2006 are available. Can anyone help with this, or can they send me useful resources to study this in depth.

Desperate for answers, I really need help. Thanks.

+6
source share
2 answers

Do you use a subclass of UIImageView and override drawRect? This is not supported (see Documents), and probably why your images are not subject to change.

If you want to do a lot of transformations and draw the drawing code, you must use a subclass of UIView and draw your image manually using CGContextDrawImage ()

Any transformations apply to the drawing operations performed after the transformation. You will see code samples in documents in which the state of the graphics is saved, conversion is applied, some kind of drawing is performed, and then the context is restored. This is how you draw some components and some not.

I hope the second part of your question will become clear after you draw your image through the CG code, and not through the image.

0
source

Source: https://habr.com/ru/post/909161/


All Articles