> I have a NSImageView in a window. In the class, I want to find out the size
> of the NSImageView so that I can allocate a NSImage of the correct size.
[quoted text clipped - 12 lines]
> I'm checking those by doing
> NSLog([NSString stringWithFormat:@"%d",vp.size]);
> you've declared 'vp' as a point, which is a struct containing two floats, but
> later you're saying 'vp.size'? there is no way that could have even compiled.
[quoted text clipped - 3 lines]
> if you're not showing us the real code, there's no way anybody here can be of
> any help.
Sorry that's my fault for stuffing up the example. Here is the method in
question, the whole class would probably be too big. What I really want to do
is remove the hard coded values in initWithSize to be equal to the size of
the NSView called userGraph. Eventually I'm going to draw bar charts into the
image, but for now it just displays randomly placed squares. The NSLog output
shows that the size reported in NSSize ms is always 1081352192 for width and
0 for height.
-(void)drawGraph{
NSImage *testImage = [[NSImage alloc] initWithSize: NSMakeSize(200,200)];
srandom(time(NULL));
NSRect r;
NSPoint p;
NSSize s;
NSRect mr;
NSSize ms;
mr=[userGraph bounds];
ms=mr.size;
[testImage lockFocus];
[[NSColor blueColor] set];
int cx,cy,my;
cx=0;cy=0;
my=(random () % 20 ) + 2;
for(cx=0;cx<200;cx+=6) {
p.x=cx;p.y=my*5;
s.width=5;s.height=5;
r.origin=p; r.size=s;
[NSBezierPath fillRect:r];
my=(random () % 20 ) + 2;
}
[[NSColor whiteColor] set];
[testImage unlockFocus];
NSLog(@"Bounds");
NSLog(@"%d %d",ms.width,ms.height);
[userGraph setImage:testImage];
}

Signature
remove stars for email
g*a*r*y*c*o*w*e*l*l*a*t*m*a*c*d*o*t*c*o*m
Patrick Machielse - 02 Sep 2005 09:52 GMT
> Sorry that's my fault for stuffing up the example. Here is the method in
> question, the whole class would probably be too big. What I really want to do
[quoted text clipped - 12 lines]
> NSLog(@"%d %d",ms.width,ms.height);
> }
OK, now show us where your userGraph is :-)
- Is it in a nib, if so is the userGraph outlet connected?
- Is is created in code, if so did you use initWithFrame:, did you add
it to a window?
etc...
patrick
Gary - 02 Sep 2005 10:37 GMT
> > Sorry that's my fault for stuffing up the example. Here is the method in
> > question, the whole class would probably be too big. What I really want to do
[quoted text clipped - 20 lines]
>
> etc...
It's in the nib, yes. With an outlet in AppController. AppController was
Control-dragged to the NSImageView and connected. The method I posted is in
AppController.m , so the source is AppController and the target was the
NSImageView, outlet named userGraph.
I wouldn't have thought that the call to [userGraph setImage:testImage]
would have worked if there was no outlet connection.
Does the NSImageView have to be a Source and AppController have a target
action for this to work?
Michael Ash - 02 Sep 2005 11:33 GMT
> Sorry that's my fault for stuffing up the example. Here is the method in
> question, the whole class would probably be too big. What I really want to do
[quoted text clipped - 3 lines]
> shows that the size reported in NSSize ms is always 1081352192 for width and
> 0 for height.
[snip]
> NSLog(@"%d %d",ms.width,ms.height);
%d is the format specifier for integers. ms.width and ms.height are
floats. That's why you're getting bizarre values. Replace these with %f
and you will see the correct numbers. Use them in your code and they will
work.

Signature
Michael Ash
Rogue Amoeba Software
Gary - 02 Sep 2005 11:52 GMT
> > NSLog(@"%d %d",ms.width,ms.height);
>
> %d is the format specifier for integers. ms.width and ms.height are
> floats. That's why you're getting bizarre values. Replace these with %f
> and you will see the correct numbers. Use them in your code and they will
> work.
I know I'm new to Objective C and Cocoa/OpenStep but after all these years,
you'd think I wouldn't mess up on some debugging code with what is
essentially a blooming printf format specifier :-(
You are correct, my reading of [myView bounds] was always working, the debug
code to print the values was stuffed!
Thanks (feeling foolish!)
Gary