根据其转换矩阵获取UIImageView的旋转和大小 [英] obtaining the rotation and size of a UIImageView based on its transformation matrices
问题描述
如果我有一个矩形UIImageView的原始变换矩阵并且该图像被缩放和旋转,并且到最后我可以读取该相同视图的最终变换矩阵,我如何计算缩放和旋转图像的数量? / p>
我想这些矩阵不知何故包含这两个信息。问题是如何提取它...
任何线索?
感谢您的帮助。
一点矩阵代数和三角函数可以帮助你解决这个问题。
我们将继续生成可缩放和旋转的矩阵,然后使用它来分析如何分析提取比例因子和旋转。
A缩放矩阵按比例缩放Sx(在X轴上)和Sy(在Y轴上)如下所示:
⎡Sx 0⎤$ b $b⎣0Sy⎦
顺时针旋转R弧度的矩阵看起来像这样:
⎡cos(R)sin(R)⎤
⎣-sin(R)cos(R)⎦
使用标准矩阵乘法,组合缩放和旋转矩阵将如下所示:
⎡Sx.cos(R)Sx.sin(R)⎤
⎣-Sy.sin(R)Sy.cos(R )⎦
请注意,线性变换还可能包括剪切或其他变换,但我会假设这个问题只发生了旋转和缩放(如果剪切变换在矩阵中,则会得到不一致的结果跟随代数;但是同样的方法可以用来确定分析解决方案。)
A CGAffineTransform 有四个成员a,b,c,d,对应到二维矩阵:
⎡ab⎤$ b $b⎣cd⎦
现在我们要从这个矩阵中提取Sx,Sy和R的值。我们可以在这里使用一个简单的三角函数:
tan(A)= sin(A)/ cos(A)
我们可以在矩阵的第一行使用它来得出结论:
tan(R)= Sx.sin(R)/ Sx.cos(R)= b / a因此R = atan(b / a)
现在我们知道R,我们可以使用主对角线提取比例因子:
a = Sx.cos(R)因此Sx = a / cos(R)
d = Sy.cos(R)因此Sy = d / cos(R)
所以你现在知道Sx,Sy和R.
If I have the original transform matrix of a rectangular UIImageView and this image is scaled and rotated and by the end I can read the final transform matrix of this same view, how can I calculate how much the image scaled and rotated?
I suppose that somehow these matrix contain these two informations. The problem is how to extract it...
any clues?
thanks for any help.
A little bit of matrix algebra and trigonometric identities can help you solve this.
We'll work forward to generate a matrix that scales and rotates, and then use that to figure out how to extract the scale factors and rotations analytically.
A scaling matrix to scale by Sx (in the X axis) and Sy (in the Y axis) looks like this:
⎡Sx 0 ⎤
⎣0 Sy⎦
A matrix to rotate clockwise by R radians looks like this:
⎡cos(R) sin(R)⎤
⎣-sin(R) cos(R)⎦
Using standard matrix multiplication, the combined scaling and rotation matrix will look like this:
⎡Sx.cos(R) Sx.sin(R)⎤
⎣-Sy.sin(R) Sy.cos(R)⎦
Note that linear transformations could also include shearing or other transformations, but I'll assume for this question that only rotation and scaling have occurred (if a shear transform is in the matrix, you will get inconsistent results from following the algebra here; but the same approach can be used to determine an analytical solution).
A CGAffineTransform has four members a, b, c, d, corresponding to the 2-dimensional matrix:
⎡a b⎤
⎣c d⎦
Now we want to extract from this matrix the values of Sx, Sy, and R. We can use a simple trigonometric identity here:
tan(A) = sin(A) / cos(A)
We can use this with the first row of the matrix to conclude that:
tan(R) = Sx.sin(R) / Sx.cos(R) = b / a and therefore R = atan(b / a)
And now we know R, we can extract the scale factors by using the main diagonal:
a = Sx.cos(R) and therefore Sx = a / cos(R)
d = Sy.cos(R) and therefore Sy = d / cos(R)
So you now know Sx, Sy, and R.
这篇关于根据其转换矩阵获取UIImageView的旋转和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!