简体   繁体   English

从CGAffineTransform中查找比例值

[英]Find Scale Value from CGAffineTransform

Ok, so I realize I can find the scale value from a layer's CATransform3D like this: 好的,所以我意识到我可以从图层的CATransform3D中找到比例值,如下所示:

 float scale = [[layer valueForKeyPath: @"transform.scale"] floatValue];

But I can't for the life of me figure out how I would find the scale value from a CGAffineTransform. 但我不能为我的生活弄清楚如何从CGAffineTransform中找到比例值。 Say for instance I have this CGAffineTransform called "cameraTransform": 比方说我有这个名为“cameraTransform”的CGAffineTransform:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
CGAffineTransform *cameraTransform =  imagePicker.cameraViewTransform;

Now how do I get the scale value from cameraTransform? 现在如何从cameraTransform获取比例值?

I try to give a general answer for all kinds of CGAffineTransforms, even rotated ones. 我尝试给出各种CGAffineTransforms的一般答案,甚至是旋转的。

Assuming your CGAffineTransform contains (optionally) 假设您的CGAffineTransform包含(可选)

  • rotation 回转
  • translation 翻译
  • scaling 缩放

and

  • NO skew 没有歪斜

then the there's a general formula that gives you the scale factor: 然后有一个通用公式,为您提供比例因子:

CGAffineTransform transform = ...;
CGFloat scaleFactor = sqrt(fabs(transform.a * transform.d - transform.b * transform.c));

"Mirroring" or flipping coordinate directions will be ignored, that means (x --> -x; y --> y) will result in scaleFactor == 1 instead of -1. “镜像”或翻转坐标方向将被忽略,这意味着(x - > -x; y - > y)将导致scaleFactor == 1而不是-1。

http://en.wikipedia.org/wiki/Determinant http://en.wikipedia.org/wiki/Determinant

"A geometric interpretation can be given to the value of the determinant of a square matrix with real entries: the absolute value of the determinant gives the scale factor by which area or volume is multiplied under the associated linear transformation, while its sign indicates whether the transformation preserves orientation. Thus a 2 × 2 matrix with determinant −2, when applied to a region of the plane with finite area, will transform that region into one with twice the area, while reversing its orientation." “可以对具有实数条目的方阵的行列式的值进行几何解释:行列式的绝对值给出在相关线性变换下乘以面积或体积的比例因子,而其符号表示是否因此,当应用于具有有限区域的平面区域时,具有行列式-2的2×2矩阵将该区域变换为具有两倍面积的区域,同时反转其方向。

The article goes on to give formulas for the determinant of a 3x3 matrix and a 2x2 matrix. 本文接着给出了3x3矩阵和2x2矩阵的行列式的公式。 CGAffineTransforms are 3x3 matrices, but their right column is always 0 0 1. The result is the determinant will be equal to the determinant of the 2x2 upper left square of the matrix. CGAffineTransforms是3x3矩阵,但它们的右列始终为0 0 1.结果是行列式将等于矩阵的2x2左上方的行列式。 So you can use the values from the struct and compute the scale yourself. 因此,您可以使用结构中的值并自己计算比例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM