How to reset to identity the “current transformation matrix” with some CGContext function?

天涯浪子 提交于 2019-12-21 11:13:30

问题


I'm doing a series of translations and rotations on the CTM and at some point I need to reset it to identity before going further with transformations.

I can't find any proper way to do it (obviously, there should have been a function named CGContextSetCTM or so) and since efficiency is the key, I don't want to use CGContextSaveGState/CGContextRestoreGState...


回答1:


Get current transformation matrix via CGContextGetCTM, invert it with CGAffineTransformInvert and multiply the current matrix by the inverted one (that's important!) with CGContextConcatCTM. CTM is now identity.




回答2:


Note that inverting the current CTM with CGAffineTransformInvert does not work if your current CTM is singular.

The obvious case is if previously CGContextConcatCTM was performed with matrix CGAffineTransformMake(0, 0, 0, 0, 0, 0).




回答3:


The save/restore operations are probably a single memory copy of a memory region comparable to the size of the identity matrix (twice or thrice the size). It might only happen for the save operation. Consider that this is probably not much slower than a nop FUNCTION call. Each graphic operation is in the scale of several multiplication operation and I'm guessing this happens more than once in your code for each save/restore cycle. The time of one graphic operation is probably larger than a single save/restore cycle.



来源:https://stackoverflow.com/questions/469505/how-to-reset-to-identity-the-current-transformation-matrix-with-some-cgcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!