NSImageView image padding?

So I'm used to UIImageView , and I can set different ways to display its image in it. Like, for example, AspectFill mode AspectFill etc ...

I would like to achieve the same using NSImageView in a Mac application. NSImageView Is NSImageView similar to UIImageView for that matter, or how do I show an image in NSImageView and choose different ways to display this image?

+14
macos nsimageview
source share
6 answers

You may find it much easier for NSView subclass NSView and provide a CALayer that does this for you. Here's what init might look like for this subclass of NSView .

 - (id)initWithFrame:(NSRect)frame andImage:(NSImage*)image { self = [super initWithFrame:frame]; if (self) { self.layer = [[CALayer alloc] init]; self.layer.contentsGravity = kCAGravityResizeAspectFill; self.layer.contents = image; self.wantsLayer = YES; } return self; } 

Note that the order in which the layer is set, and then the wantLayer setting is very important (if you first want wantLayer, you will get the default backing layer instead).

You can have a setImage method that simply updates the contents of the layer.

+21
source share

I had the same problem. I wanted the image to scale to fill, but keep the aspect ratio of the original image. Oddly enough, it is not as simple as it seems, and does not fail with NSImageView. I really wanted the NSImageView scale to change beautifully with the help of a supervisor. I created an NSImageView subdirectory that you can find on github: KPCScaleToFillNSImageView

+13
source share

You can use this: the image will force the view size

(Aspect of filling)

 imageView.imageScaling = .scaleAxesIndependently 


(Aspect Fit)

 imageView.imageScaling = .scaleProportionallyUpOrDown 


(Center on top)

 imageView.imageScaling = .scaleProportionallyDown 

This works for me.

+4
source share

Here is what I use written with Swift. This approach works well for storyboards - just use a regular NSImageView, and then replace the NSImageView name in the Class field with MyAspectFillImageNSImageView ...

 open class MyAspectFillImageNSImageView : NSImageView { open override var image: NSImage? { set { self.layer = CALayer() self.layer?.contentsGravity = kCAGravityResizeAspectFill self.layer?.contents = newValue self.wantsLayer = true super.image = newValue } get { return super.image } } } 
+4
source share

To get the AspectFit property of a UIImageView in Cocoa NSImageView use this method

 [imageView setImageScaling:NSScaleProportionally]; 

Here you can change the display properties of the image.

 enum { NSScaleProportionally = 0, // Deprecated. Use NSImageScaleProportionallyDown NSScaleToFit, // Deprecated. Use NSImageScaleAxesIndependently NSScaleNone // Deprecated. Use NSImageScaleNone }; 
+1
source share

I had difficulty trying to figure out how to make Aspect Fill Clip to Bounds :

Credit: https://osxentwicklerforum.de/index.php/Thread/28812-NSImageView-Scaling-Seitenverh%C3%A4ltnis/ Image courtesy: https://osxentwicklerforum.de/index.php/Thread/28812-NSImageView-Scaling-Seitenverh%C3%A4ltnis/

Finally, I created my own subclass of NSImageView, hope this can help someone:

 import Cocoa @IBDesignable class NSImageView_ScaleAspectFill: NSImageView { @IBInspectable var scaleAspectFill : Bool = false override func awakeFromNib() { // Scaling : .scaleNone mandatory if scaleAspectFill { self.imageScaling = .scaleNone } } override func draw(_ dirtyRect: NSRect) { if scaleAspectFill, let _ = self.image { // Compute new Size let imageViewRatio = self.image!.size.height / self.image!.size.width let nestedImageRatio = self.bounds.size.height / self.bounds.size.width var newWidth = self.image!.size.width var newHeight = self.image!.size.height if imageViewRatio > nestedImageRatio { newWidth = self.bounds.size.width newHeight = self.bounds.size.width * imageViewRatio } else { newWidth = self.bounds.size.height / imageViewRatio newHeight = self.bounds.size.height } self.image!.size.width = newWidth self.image!.size.height = newHeight } // Draw AFTER resizing super.draw(dirtyRect) } } 

Plus it's @IBDesignable so you can install it on StoryBoard

WARNINGS

  • I am new to MacOS Swift development, I came from iOS development, so I was surprised that I could not find the clipToBound property, maybe it exists, and I could not find it!

  • As for the code, I suspect that it consumes a lot and also has a side effect for changing the original image ratio over time. This side effect seemed insignificant to me.

Once again, if this is a setting that allows NSImageView crop borders, remove this answer:]

0
source share

All Articles