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.