• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSImage origin
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSImage origin


  • Subject: Re: NSImage origin
  • From: "John C. Randolph" <email@hidden>
  • Date: Wed, 22 Sep 2004 17:05:03 -0700


On Sep 22, 2004, at 2: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];
Off the top of my head, try adding:    

NSPoint myOrigin  = [self bounds].origin;
[[[NSAffineTransform transform]  translateXBy: - myOrigin.x yBy: -myOrigin.y]  concat];

[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.)
What's tripping you up here is that when you lock focus on an NSImage, you have a drawing context, but you don't have a focused NSView.  That means that any of the view methods for translating the coordinate space are unavailable, and any changes you may have made to the coordinate space in your view aren't applied to the current drawing context.

What you'll need to do to move the origin when you're drawing on an image, is either to use the CGContextTranslateCTM() function, or use an NSAffineTransform object as I've done above. 

-jcr

On Sep 22, 2004, at 2: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];
Off the top of my head, try adding:    

NSPoint myOrigin  = [self bounds].origin;
[[[NSAffineTransform transform]  translateXBy: - myOrigin.x yBy: -myOrigin.y]  concat];


[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.)
What's tripping you up here is that when you lock focus on an NSImage, you have a drawing context, but you don't have a focused NSView.  That means that any of the view methods for translating the coordinate space are unavailable, and any changes you may have made to the coordinate space in your view aren't applied to the current drawing context.


What you'll need to do to move the origin when you're drawing on an image, is either to use the CGContextTranslateCTM() function, or use an NSAffineTransform object as I've done above. 

-jcr


John C. Randolph <email@hidden> (408) 974-8819 Sr. Cocoa Software Engineer, Apple Worldwide Developer Relations http://developer.apple.com/cocoa/index.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
  • Follow-Ups:
    • Re: NSImage origin
      • From: Don Willems <email@hidden>
References: 
 >NSImage origin (From: Don Willems <email@hidden>)

  • Prev by Date: Re: Newbie question: how to release an object that has been autoreleased
  • Next by Date: Re: Newbie UndoManager question
  • Previous by thread: NSImage origin
  • Next by thread: Re: NSImage origin
  • Index(es):
    • Date
    • Thread