2
votes

I have multiple NSImageView of different dimensions and proportions that are stacked vertically in a view that has limited horizontal space.

Since the image dimensions can be larger than the size of the parent view I want to scale them down proportionally by using NSImageScaleProportionallyDown.

The problem for me seems to be the default behaviour of NSImageViews where it only scales down the axis that exceeds the constraints set by auto layout and then centers the NSImage in the NSImageView, this causes the NSImageView to take up as much height as if it was not scaled down at all and breaks the vertical flow since the space between the images vary.

I have tried to set [questionImage setContentHuggingPriority:NSLayoutPriorityRequired forOrientation:NSLayoutConstraintOrientationHorizontal]; without any success.

So how do you make the NSImageView only take up as much space as the proportionally scaled NSImage requires?

2

2 Answers

3
votes

So how do you make the NSImageView only take up as much space as the proportionally scaled NSImage requires?

You need to know the aspect ratio of the image, and set the NSImageView's width constraint multiplier with respect to its height constraint (or vice versa) so as to enforce that same aspect ratio. In other words, if the image view's width is being determined by external constraints, then its height needs to be determined (through its multiplier) by its resulting width, or vice versa.

0
votes

I don't know the answer to your question, but would like to suggest an alternative approach, if at all possible in your use case. How about you scale down the real images before placing them in the image views. This way you will also save some memory as the content of the image views will itself be smaller. You can find an example I provided for scaling images on iOS. I made a quick check and it appears you can write analogous code for OSX. See In iOS, how to convert a web image into a less-quality one? for the example.