Re: Using NSImage to render the view
Re: Using NSImage to render the view
- Subject: Re: Using NSImage to render the view
- From: Tony Romano <email@hidden>
- Date: Thu, 01 Jul 2010 19:20:42 -0700
Thanks for the tip.
I figured it was too good to be true that the image would scale.
Thinking about it, the class hierarchy only shows NSObject, I should
have looked there to know for sure. I'm not tied to this method, it
just works nicely. I'll covert the coordinates to the view's bounding
rectangle and not rely on scaling. Thanks for the detailed reply Graham.
-Tony
On 7/1/10 6:55 PM, Graham Cox wrote:
On 02/07/2010, at 11:29 AM, Tony Romano wrote:
I have a view who's bounding rectangle has been normalize to make drawing a grid easier. It' bounds is set to ~1x1. Currently I can draw the grid and what ever curve I want just fine. What I am trying to do is draw into a NSImage and render it in the view and cache the image so I can also save the image to disk later. In my test example I am drawing a curve and to see if the curve renders prior to unlockFocus(i.e post the last stroke method) I make a call to:
NSRect br = [bp controlPointBounds];
NSLog(@"Path Bounding Box: origin.x:%1.5f origin.y:%1.5f size.width:%1.5f size.height:%1.5f", br.origin.x, br.origin.y, br.size.width, br.size.height);
Tip: check out NSStringFromRect()
The Output I get is: Path Bounding Box: origin.x:0.00000 origin.y:0.00000 size.width:1.00000 size.height:1.00000. Looks like the path is drawn just fine.
The documents look pretty straight forward but it looks like I am only getting a 1x1 view of my data(i.e. the pixel(s) at 0,0) and losing the scaling factor of the frame.
I am assuming the same trick to normalize a view to 1x1 should also work?? Again, if I just draw to the view and not to the image, it all draws perfectly.
You are assuming wrong. You create an image with size 1,1 and draw into it data with bounds 1,1. You therefore have a 1x1 pixel image with all the complexity and nuance that implies.
The reason the "trick" works with a view is that the view incorporates a transform that scales between the bounds and the frame rect, so the data you draw to the 1,1 bounds is scaled up to fill the n,m frame. The scaled path is stroked on the fly to fill the resulting part of the screen. NSImage has no similar automatic transformations.
To my mind your approach is flawed, I'm not sure why you think having a 1,1 view is needed or why that's any advantage over doing things conventionally, but if you want to persist with it, you need to make the image the size of your frame and set up a suitable transform yourself when you lock focus on the image to scale the data up the same way the view is.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden