如何重置以识别“当前变换矩阵” 有一些 CGContext 函数吗?
我正在 CTM 上进行一系列平移和旋转,在某些时候我需要将其重置为身份,然后再进一步进行转换。
我找不到任何正确的方法来做到这一点(显然,应该有一个名为 CGContextSetCTM 左右的函数),并且由于效率是关键,所以我不想使用 CGContextSaveGState/CGContextRestoreGState...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过 获取当前变换矩阵CGContextGetCTM,使用 CGAffineTransformInvert 并将当前矩阵乘以反转矩阵(这很重要!) CGContextConcatCTM。 CTM 现在是身份。
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.
保存/恢复操作可能是与单位矩阵的大小(大小的两倍或三倍)相当的内存区域的单个内存副本。 它可能只发生在保存操作中。 考虑一下这可能并不比 nop FUNCTION 调用慢多少。 每个图形操作都在多个乘法操作的范围内,我猜测在每个保存/恢复周期的代码中这种情况会发生不止一次。 一个图形操作的时间可能比单个保存/恢复周期还要长。
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.
请注意,如果当前 CTM 是奇异的,则使用 CGAffineTransformInvert 反转当前 CTM 不起作用。
明显的情况是,如果之前使用矩阵 CGAffineTransformMake(0, 0, 0, 0, 0, 0) 执行 CGContextConcatCTM。
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).