1

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?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393

1 Answers1

2

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.

Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • I can't really think of any examples of when I would ever *need* to use an Affine transform. Are there any? – Suragch Jun 10 '15 at 14:01
  • Well you might want some custom methods such as inverted matrix or interpolations on the context. Otherwise you may have brought it from another component such as an UIView or simply another context or just the other way around... Anyway there are quite a few reasons I can think of. – Matic Oblak Jun 10 '15 at 14:06
  • can you please explain argument parameters in details. – Vikash Rajput Jul 06 '16 at 09:02
  • Which of them are you interested in beyond the documentation? The translation matrix A is the identity modified so that the last row (column for some implementations) is set to the translation vector where the given matrix B is multiplied as B*A or A*B (SADLY depends on implementation) but the 2 arguments are the representation of a 2d vector for which the object translation happens... In other words if you were the object and you were said ti translate (x,y) it means go x steps right and y steps up no matter how small you were or what way you were facing. – Matic Oblak Jul 06 '16 at 18:17