Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSImageView image aspect fill?

Tags:

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?

like image 610
zumzum Avatar asked Apr 11 '14 02:04

zumzum


2 Answers

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.

like image 200
Chris Demiris Avatar answered Oct 20 '22 17:10

Chris Demiris


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         }     }  } 
like image 43
Pete Avatar answered Oct 20 '22 16:10

Pete