• 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: Drawing views into NSImage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Drawing views into NSImage


  • Subject: Re: Drawing views into NSImage
  • From: "Geert B. Clemmensen" <email@hidden>
  • Date: Tue, 5 Apr 2005 10:03:06 +0200

Hello,

As usual, jcr's help turned out to be invaluable.

I would expect that when you tried to draw more than one view on the same image, they all tried to draw in the lower-left corner?

Yes, although I could use NSAffineTransform to "move" 0,0, but never in a consistent way.

To find out where to put the origin, you can probably just send -convertPoint:toView: to each subview you want to draw,

Here the light bulb got turned on. Normally, a drawRect: method can safely assume that focus has been locked, clipping is set up, and lower-left is 0,0. When drawing into an NSImage, none of these assumptions are applicable, the upside is that the drawRect: method pretty much can draw wherever it wants. In the given case, the subviews I wanted drawn into an NSImage are actually custom NSControls, each with two NSTextFieldCell's and one NSButtonCell. I then wrote a drawRectIntoImage:(NSPoint) newOrigin method which will make sure that drawing happens at appropriate place inside the NSImage.

Now things work like a charm.

Regards,
Geert B. Clemmensen
www.frontbase.com


On 4. apr 2005, at 20.13, John C. Randolph wrote:

On Apr 3, 2005, at 11:27 PM, Geert B. Clemmensen wrote:

Hello,

I have N views (or more specifically, N subviews of a content view that holds M subviews with M >= N). I want to draw these N views into an NSImage. I calculate the proper rect and initializes an NSImage instance accordingly, i.e. its size matches that of the calculated rect. I then do:

    [myImage lockFocus];
        // fill the image with clearColor
        // draw the N views via drawRect: calls
    [myImage unlockFocus];

This kinda works, i.e. I get an NSImage that almost looks right, but I haven't been able to consistently have the N views draw so their relative position to each other is kept. I have tried enough variants of NSAffineTransform with -concat/-set (with and without save/restoreGraphicsState) to realize that this may not be the way to go. What is the recommended/preferred way to change the coordinate system before each call to drawRect:?

I believe I have googled and otherwise searched the topic quite extensively, but haven't yet found a solution. Any pointers will be highly appreciated.

What's tripping you up here is most likely the fact that when the drawing desination isn't an NSView, NSView's bounds and frame manipulation methods are no-ops.

I would expect that when you tried to draw more than one view on the same image, they all tried to draw in the lower-left corner?

If the  view in question is visible in a window on screen, you should be able to get what you want with NSBitmapImageRep's -initWithFocusedViewRect:, or NSView's -dataWithPDFInsideRect: methods.  If the view isn't in a window already, then  what you'll need to do is draw the views in their view hierarchy order, and manually set up the coordinate space (and clipping) before  drawing each one.  To find out where to put the origin, you can probably just send -convertPoint:toView: to each subview you want to draw, and discover any scaling using -convertSize:toView: using a size of { 1.0,  1.0 }.

-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

References: 
 >Drawing views into NSImage (From: "Geert B. Clemmensen" <email@hidden>)
 >Re: Drawing views into NSImage (From: "John C. Randolph" <email@hidden>)

  • Prev by Date: PreferencePane plugin: awakeFromNib gets called twice
  • Next by Date: Re: Best way to use bindings with NSMatrix
  • Previous by thread: Re: Drawing views into NSImage
  • Next by thread: Unicode Keyboard Layout
  • Index(es):
    • Date
    • Thread