So I am used to UIImageView
, and being able to set different ways of how its image is displayed in it. Like for example AspectFill
mode etc...
I would like to accomplish the same thing using NSImageView
on a mac app. Does NSImageView
work similarly to UIImageView
in that regard or how would I go about showing an image in an NSImageView
and picking different ways of displaying that image?
You may find it much easier to subclass NSView
and provide a CALayer
that does the aspect fill for you. Here is what the init
might look like for this NSView
subclass.
- (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 of setting the layer, then settings wantsLayer is very important (if you set wantsLayer first, you'll get a default backing layer instead).
You could have a setImage
method that simply updates the contents of the layer.
Here is what I'm using, written with Swift. This approach works well with storyboards - just use a normal NSImageView, then replace the name NSImageView in the Class box, 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 } } public override init(frame frameRect: NSRect) { super.init(frame: frameRect) } //the image setter isn't called when loading from a storyboard //manually set the image if it is already set required public init?(coder: NSCoder) { super.init(coder: coder) if let theImage = image { self.image = theImage } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With