Re: Using NSImage to render the view
Re: Using NSImage to render the view
- Subject: Re: Using NSImage to render the view
- From: Graham Cox <email@hidden>
- Date: Fri, 2 Jul 2010 11:55:42 +1000
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