Get width and height of NSView and NSImageView

I have an NSView, in NSView there is an NSImageView

in NSView source code

I wrote:

NSImage *image = [[[NSImage alloc] initWithContentsOfURL:url] autorelease]; NSImageView *newImageView = nil; newImageView = [[NSImageView alloc] initWithFrame:[self bounds]]; [newImageView setImage:image]; [newImageView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; 

Is there a way to read the width and height of NSView and NSImageView?

+4
source share
2 answers

The size of any view (NSView, NSImageView, etc.) is view.frame.size or view.bounds.size . In both cases, this is an identical NSSize structure. You can write code like this:

 NSSize size = newImageView.frame.size; NSLog(@"size: %@", NSStringFromSize(size)); NSLog(@"size: %fx %f", size.width, size.height); 

To change it, you need to update the view frame property:

 NSRect frame = view.frame; frame.size = NSMakeSize(<#new width#>, <#new height#>); view.frame = frame; 
+7
source

Try the - (NSRect)bounds method.

+2
source

All Articles