CTM transforms vs Affine Transforms in iOS (for translate, rotate, scale)

1.1k views Asked by At

I read through the Transforms documentation in the Quartz 2D Programming Guide. In it there appears to be two ways to make transformations. One way is through modifying the Current Transformation Matrix (CTM). It has methods like the following:

  • CGContextTranslateCTM
  • CGContextRotateCTM
  • CGContextScaleCTM

The other way is to use Affine transforms. It has methods like the following:

  • CGAffineTransformTranslate
  • CGAffineTransformRotate
  • CGAffineTransformScale

The docs state

The affine transform functions available in Quartz operate on matrices, not on the CTM.

But I don't understand how that affects me practically. It seems like I can get the same result using either method. When should I use the CTM transforms and when should I use the Affine transforms?

1

There are 1 answers

4
Matic Oblak On BEST ANSWER

CTM is a current transformation matrix and the CTM methods will make operations on the current matrix.

The other version of functions will make the transformation on a given matrix which means you need to specify which matrix you are trying to transform. After you did so you may apply the transform to the CTM any way you want or use it for any other purpose.

For instance these 2 operations would be the same:

CGContextTranslateCTM(context, 10, 10);

Affine:

CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, 10, 10);
CGContextConcatCTM(context, transform);

As you can see the first one is more or less just a convenience so you do not need to write so much code.