Re: Problems with flipping an NSImage
Re: Problems with flipping an NSImage
- Subject: Re: Problems with flipping an NSImage
- From: "Shawn Erickson" <email@hidden>
- Date: Tue, 17 Oct 2006 11:57:21 -0700
On 10/17/06, Aychamo Aychamo <email@hidden> 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
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden