NSAffineTransform question

  • Hello all,

      I'm using NSAffineTransform to draw a rotated image in
    a view. When I delete the NSAffineTransform part
    in my code, I get the results i expect, but when I add the NSAffineTransform
    instructions I see nothing at all in the view.

      The relevant snippet (in the drawRect: method) goes :

    /*version 1 : without the NSAffineTransform stuff */

    [theImage drawInRect:drawingRect
                 fromRect:imageRect
                 operation:NSCompositeSourceOver
                 fraction:1.0];
    /*version 2 */
                            NSAffineTransform* xform=[NSAffineTransform
    transform];
      [xform rotateByDegrees:90];
      [xform concat];
      [theImage drawInRect:drawingRect
                 fromRect:imageRect
                 operation:NSCompositeSourceOver
                 fraction:1.0];
      [xform invert];
      [xform concat];

    Apart from that region, my code is identical
    in the two cases. Did I put the instructions in the wrong order ?

                                              Ewan
  • Right now the image is rotated around (0,0). To fix this problem:

    float deltaX = imageRect.size.width * 0.5;
    float deltaY = imageRect.size.height * 0.5;

    [xform translateXBy:deltaX yBy:deltaY];
    [xform rotateByDegrees:90];
    [xform translateXBy:-deltaX yBy:-deltaY];

    On 4/25/07, Ewan Delanoy <delanoy...> wrote:
    > Hello all,
    >
    > I'm using NSAffineTransform to draw a rotated image in
    > a view. When I delete the NSAffineTransform part
    > in my code, I get the results i expect, but when I add the NSAffineTransform
    > instructions I see nothing at all in the view.
    >
    > The relevant snippet (in the drawRect: method) goes :
    >
    > /*version 1 : without the NSAffineTransform stuff */
    >
    > [theImage drawInRect:drawingRect
    > fromRect:imageRect
    > operation:NSCompositeSourceOver
    > fraction:1.0];
    > /*version 2 */
    > NSAffineTransform* xform=[NSAffineTransform
    > transform];
    > [xform rotateByDegrees:90];
    > [xform concat];
    > [theImage drawInRect:drawingRect
    > fromRect:imageRect
    > operation:NSCompositeSourceOver
    > fraction:1.0];
    > [xform invert];
    > [xform concat];
    >
    > Apart from that region, my code is identical
    > in the two cases. Did I put the instructions in the wrong order ?
    >
    > Ewan
    >
  • Janek Priimann wrote:

    > Right now the image is rotated around (0,0). To fix this problem:
    >
    > float deltaX = imageRect.size.width * 0.5;
    > float deltaY = imageRect.size.height * 0.5;
    >
    > [xform translateXBy:deltaX yBy:deltaY];
    > [xform rotateByDegrees:90];
    > [xform translateXBy:-deltaX yBy:-deltaY];

    There's actually a bit more to do here. The above code will work if
    the image is square and the lower-left corner of the result is
    intended to be at the origin. Since we're dealing with a multiple of
    90 degrees, you can just swap the x and y offsets for the final
    translate (which is, for the OP, the first one in the code line). But
    the offsets should also be adjusted by the lower-left corner of the
    intended destination rectangle. And the destination rectangle itself
    should not be modified ahead of time to reflect the rotated dimensions.

    Ewan Delanoy wrote:

    > [xform concat];
    > [theImage drawInRect:drawingRect fromRect:imageRect
    > operation:NSCompositeSourceOver fraction:1.0];
    > [xform invert];
    > [xform concat];

    Those last two lines aren't a reliable way to remove the transform.
    I've seen what I consider trivial transforms fail to invert. If you
    want to impose a transform just for a little while:

    [NSGraphicsContext saveGraphicsState];
    [theTransform concat];
    // draw, draw, draw
    [NSGraphicsContext restoreGraphicsState];
previous month april 2007 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
Go to today