Re: Using NSImage to render the view

2010-07-01 Thread Tony Romano

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.0 origin.y:0.0 
size.width:1.0 size.height:1.0. 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Using NSImage to render the view

2010-07-01 Thread Graham Cox

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.0 origin.y:0.0 
> size.width:1.0 size.height:1.0. 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Using NSImage to render the view

2010-07-01 Thread Tony Romano
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);


The Output I get is: Path Bounding Box: origin.x:0.0 
origin.y:0.0 size.width:1.0 size.height:1.0.  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.


This is what I am doing.

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
viewBounds = NSMakeRect(-0.1, -0.1, 1.2, 1.2);
[self setBounds:viewBounds];
}
return self;
}

-(void) setFrameSize:(NSSize)newSize
{
[super setFrameSize:newSize];
[self setBounds:viewBounds];
}

- (void)drawRect:(NSRect)dirtyRect
{

// Set the image size to the bounding rect
NSImage* anImage = [[NSImage alloc] initWithSize:viewBounds];
[anImage lockFocus];

// Draw a bunch of stuff



// Unlock and draw the image
[anImage unlockFocus];
[anImage drawAtPoint:NSMakePoint(0, 0)
fromRect: NSZeroRect
   operation: NSCompositeSourceOver
fraction: 1.0];

 [anImage release];  // for now.
}

Thanks people,
-Tony
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com