Re: NSImage origin
Re: NSImage origin
- Subject: Re: NSImage origin
- From: "R. Scott Thompson" <email@hidden>
- Date: Mon, 27 Sep 2004 14:39:06 -0500
On Sep 22, 2004, at 4:21 PM, Don Willems wrote:
Hi,
Is it possible to change the origin of an NSImage?
My problem is this:
I have a custom NSView in which I can draw. I want to create an image
(on file) of the part of the custom view in which I have drawn. For
instance I have a view of 800x600 pixels. I have drawn a figure in the
rectangle with origin (100,150) and with size 100x100. The result
should be an image of size 100x100 with the same content as the
rectangle in the NSView. The coordinates (0,0) in the image should
correspond with (100,150) in the NSView.
I know how I can translate the origin in an NSView, but I don't know
how to do it in an image.
This is my code, where cropped is the rectangle in which I have drawn
and which may have an origin other than (0,0).
image = [[[NSImage alloc] initWithSize:cropped.size]
autorelease];
[image lockFocus];
[self drawViewRect:cropped];
[image unlockFocus];
At this moment the result is an image with the same content as the
NSView in the rectangle (0,0) and size 100x100. (Which I would expect,
but don't want.)
The NSImage doesn't really have an origin, but the graphics context
that is active after the lockFocus call does. What you probably need
to do is translate that context after the lockFocus call. It would
look something like this:
image = [[[NSImage alloc] initWithSize:cropped.size] autorelease];
[image lockFocus];
[NSGraphicsContext saveGraphicsState];
[[[NSAffineTransform transform] translateXBy: -cropped.origin.x yBy:
-cropped.origin.y] concat];
[self drawViewRect:cropped];
[NSGraphicsContext restoreGraphicsState];
[image unlockFocus];
The negative signs are there because you want to move the origin down
and to the left so that when the view is drawn your "cropped" rectangle
has moved up (and to the right) to the position you want to draw.
Try it and let me know how it works.
Scott
--
Macintosh Software Engineering Consulting Services
Visit my resume at <http://homepage.mac.com/easco/RSTResume.html>
_______________________________________________
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