Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / December 2005



Tip: Looking for answers? Try searching our database.

Obtaining size of an NSImageView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gary - 01 Sep 2005 16:02 GMT
(appologies if this posts twice, I don't normally use OE)

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.

I've tried code like this, the NSImageView is myView:

NSRect v;
NSPoint vp;
NSSize vs;
v=[myView bounds];
vs=v.size;
vp=v.origin;

If I then check vs.width or vs.height, they always seem to be zero.

I'm checking those by doing
NSLog([NSString stringWithFormat:@"%d",vp.size]);

How do I know how large to make my NSImage to fit within the NSImageView?

I'm allocating the image with

NSImage *myImage = [[NSImage alloc] initWithSize: NSMakeSize(50,50)];

I'd like to replace the hard coded values with something more intelligent.

Of course, if I'm barking up a tree in completley the wrong forrest, I'd be
happy to know that as well!

G
Jhnny Fvrt (it means "wa wa dance") - 01 Sep 2005 22:56 GMT
> 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.
if you meant 'v.size', it's still wrong, because 'v.size' is also a struct
with two floats, and formatting it as "%d" is definitely not going to work.

if you're not showing us the real code, there's no way anybody here can be of
any help.
Gary - 02 Sep 2005 08:04 GMT
> 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
John C. Randolph From: - 28 Dec 2005 11:03 GMT
>(appologies if this posts twice, I don't normally use OE)

>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.

>I've tried code like this, the NSImageView is myView:

>NSRect v;
>NSPoint vp;
>NSSize vs;
>v=[myView bounds];
>vs=v.size;
>vp=v.origin;

>If I then check vs.width or vs.height, they always seem to be zero.

>I'm checking those by doing
>NSLog([NSString stringWithFormat:@"%d",vp.size]);

You're trying to print a floating point value as an integer.  Change "%d" to "%f"
in the format string above.

-jcr
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.