Find scale value from CGAffineTransform

So, I understand that I can find the scale value from the CATransform3D layer as follows:

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

But I can’t understand for life how I will find the scale value from CGAffineTransform. Say, for example, I have this CGAffineTransform called "cameraTransform":

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

Now, how do I get the scale value from cameraTransform?

+7
source share
3 answers

I am trying to give a general answer for all kinds of CGAffineTransforms, even with rotated ones.

Assuming your CGAffineTransform contains (optional)

  • rotation
  • transfer
  • Scaling

and

  • NO skew

then theres a general formula that gives you a scale factor:

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

Mirroring or flipping coordinates will be ignored, which means that (x → -x; y → y) will result in scaleFactor == 1 instead of -1.

+6
source

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

“A geometric interpretation can be given to the determinant value of a square matrix with real elements: the absolute value of the determinant gives a scale factor by which the area or volume is multiplied by the corresponding linear transformation, and its sign indicates whether the transformation preserves orientation. Thus, the 2 × 2 matrix determinant -2, applied to a region of a plane with a finite area, converts that region into a unit with doubled area, changing its orientation. "

The following are formulas for the determinant of a 3x3 matrix and a 2x2 matrix. CGAffineTransforms are 3x3 matrices, but their right column is always 0 0 1. As a result, the determinant will be equal to the determinant of the upper left square of the 2x2 matrix. This way you can use the values ​​from the structure and calculate the scale yourself.

+2
source

All Articles