Problems with flipping an NSImage

  • Hello

    I'm trying to flip an NSImage so that it will be upside down (...)
    I've searched the list's archives and have seen this done dozens of
    times.  I've tried all their code, and it still wont' work for me.  I
    must be doing something horribly wrong, but I can't seem to spot it.

    The NSImage is drawn inside of one of my NSView subclasses -drawRect
    method as follows:

    // result is an NSImage that contains what I want to be flipped...

    [result lockFocus];
    NSAffineTransform *transform = [NSAffineTransform transform];
    [transform scaleXBy: -1 yBy: 1];
    [transform concat];
    [result drawAtPoint:NSMakePoint(0,0) fromRect:NSMakeRect(0,0, [result
    size].width, [result size].height) operation:NSCompositeCopy
    fraction:1.0];
    [result unlockFocus];

    [result drawAtPoint: NSMakePoint(0,0) fromRect: NSZeroRect operation:
    NSCompositeSourceOver fraction: 1.0];

    // When result draws at the end, on the screen, it appears as normal,
    as if it has not been flipped.

    Does anyone know what I'm doing wrong?

    Thank you
    AA
  • Le Oct 17, 2006 à 6:47 PM, Aychamo Aychamo a écrit :

    > Hello
    >
    > I'm trying to flip an NSImage so that it will be upside down (...)
    > I've searched the list's archives and have seen this done dozens of
    > times.  I've tried all their code, and it still wont' work for me.  I
    > must be doing something horribly wrong, but I can't seem to spot it.
    >
    > The NSImage is drawn inside of one of my NSView subclasses -drawRect
    > method as follows:
    >
    > // result is an NSImage that contains what I want to be flipped...
    >
    > [...]
    >
    > Thank you

    [myImage setFlipped:YES];

    ?
  • Where in the code would I put that?  Before it draws the image to the
    screen, or after?  And would it be while the image has -lockFocus?

    Thank you
    AA

    On 10/17/06, Stephane <ssudre...> wrote:
    >
    > Le Oct 17, 2006 à 6:47 PM, Aychamo Aychamo a écrit :
    >
    >> Hello
    >>
    >> I'm trying to flip an NSImage so that it will be upside down (...)
    >> I've searched the list's archives and have seen this done dozens of
    >> times.  I've tried all their code, and it still wont' work for me.  I
    >> must be doing something horribly wrong, but I can't seem to spot it.
    >>
    >> The NSImage is drawn inside of one of my NSView subclasses -drawRect
    >> method as follows:
    >>
    >> // result is an NSImage that contains what I want to be flipped...
    >>
    >> [...]
    >>
    >> Thank you
    >
    > [myImage setFlipped:YES];
    >
    > ?
    >
    >
  • You replace all of your transformation code with it.

    NSImage *pants = [[NSImage alloc] initWithWhatever:blah];
    [pants setFlipped:YES];

    Then do whatever you like with your image.

    --
    mikey

    On 17 Oct, 2006, at 12:58, Aychamo Aychamo wrote:

    > Where in the code would I put that?  Before it draws the image to the
    > screen, or after?  And would it be while the image has -lockFocus?
    >
    > Thank you
    > AA
    >
    > On 10/17/06, Stephane <ssudre...> wrote:
    >>
    >> Le Oct 17, 2006 à 6:47 PM, Aychamo Aychamo a écrit :
    >>
    >>> Hello
    >>>
    >>> I'm trying to flip an NSImage so that it will be upside down (...)
    >>> I've searched the list's archives and have seen this done dozens of
    >>> times.  I've tried all their code, and it still wont' work for
    >> me.  I
    >>> must be doing something horribly wrong, but I can't seem to spot
    >> it.
    >>>
    >>> The NSImage is drawn inside of one of my NSView subclasses -
    >> drawRect
    >>> method as follows:
    >>>
    >>> // result is an NSImage that contains what I want to be flipped...
    >>>
    >>> [...]
    >>>
    >>> Thank you
    >>
    >> [myImage setFlipped:YES];
    >>
    >> ?
    >>
    >>
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/mikey-san%
    > 40bungie.org
    >
    > This email sent to <mikey-san...>
  • On 10/17/06, Aychamo Aychamo <aychamo...> wrote:
    > Hello
    >
    > I'm trying to flip an NSImage so that it will be upside down (...)
    > I've searched the list's archives and have seen this done dozens of
    > times.  I've tried all their code, and it still wont' work for me.  I
    > must be doing something horribly wrong, but I can't seem to spot it.
    >
    > The NSImage is drawn inside of one of my NSView subclasses -drawRect
    > method as follows:
    >
    > // result is an NSImage that contains what I want to be flipped...
    >
    > [result lockFocus];
    > NSAffineTransform *transform = [NSAffineTransform transform];
    > [transform scaleXBy: -1 yBy: 1];
    > [transform concat];
    > [result drawAtPoint:NSMakePoint(0,0) fromRect:NSMakeRect(0,0, [result
    > size].width, [result size].height) operation:NSCompositeCopy
    > fraction:1.0];
    > [result unlockFocus];
    >
    > [result drawAtPoint: NSMakePoint(0,0) fromRect: NSZeroRect operation:
    > NSCompositeSourceOver fraction: 1.0];
    >
    > // When result draws at the end, on the screen, it appears as normal,
    > as if it has not been flipped.
    >
    > Does anyone know what I'm doing wrong?

    Yeah I see three things:

    1) You are flipping along the X axis not the Y axis.
    2) You are not translating what you flip so you are drawing out of bounds.
    3) You are trying to draw the flipped image on top of itself instead
    of just drawing it directly.

    Try something like the following (written in email and not tested)...

    -(void) drawRect:(NSRect)rect {

        NSSize resultImageSize = [resultImage size];

        // apply y-axis flip transform to current context...
        NSAffineTransform* transform = [NSAffineTransform transform];
        [transform translateXBy:0.0 yBy:resultImageSize.height];
        [transform scaleXBy:1.0 yBy:-1.0];
        [transform concat];

        [result drawAtPoint:NSMakePoint(0,0) fromRect:NSZeroRect
    operation:NSCompositeSourceOver fraction:1.0];

        // undo transform...
        [transform invert];
        [transform concat];

        ... do other drawing (note you don't need to undo the transform if
    you have no other drawing taking place after drawing the image ...
    }

    -Shawn
previous month october 2006 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 31          
Go to today