Search code examples
objective-cioscgaffinetransform

CGRectApplyAffineTransform not doing anything?


I'm new to the CGAffine world. Anyone know why my transformation doesn't work/take ?

  1. I have a valid PDFContext created with UIGraphicsBeginPDFContextToData and the following current matrix:

    CGAffineTransform curMat = CGContextGetCTM(context);
    NSLog (@"current context matrix: %f %f %f %f %f %f", curMat.a, curMat.b, curMat.c, curMat.d, curMat.tx, curMat.ty);
    
  2. NSLog values:

    current matrix: 1.000000 0.000000 -0.000000 -1.000000 0.000000 792.000000

  3. I create the transformation matrix with:

    CGAffineTransform xform = CGAffineTransformMake(a, b, c, d, tx, ty);
    
  4. NSLog values:

    transform matrix: 0.062500 0.000000 0.000000 0.062500 0.000000 0.000000

  5. I get the my rect from:

    CGRect pdfBounds = UIGraphicsGetPDFContextBounds();
    
  6. NSLog values:

    pdfBounds: 0.000000 0.000000 612.000000 792.000000

  7. I apply the transformation matrix with:

     CGRectApplyAffineTransform (pdfBounds, xform);
    
  8. Then I re-check the current matrix value with:

    CGAffineTransform curMat2 = CGContextGetCTM(context);
    NSLog (@"current context matrix after transformation: %f %f %f %f %f %f", curMat2.a, curMat2.b, curMat2.c, curMat2.d, curMat2.tx, curMat2.ty)
    
  9. and the NSLog values are the same as at the beginning.

    current context matrix after transformation: 1.000000 0.000000 -0.000000 -1.000000 0.000000 792.000000

What am I missing ? Why is the transformation not happening/taking ? Thank you.


Solution

  • You are ignoring the result of the applied transformation and you are also not applying any transformation to the context.

    Step 7 should be:

    CGContextConcatCTM(context, xform);