Re: UIImageView Scaling


Steve Christensen <punster@...>
 

Setting only edge constraints (top/bottom/leaing/trailing) on the image view will not guarantee "consistent" size. Unlike UILabel, for example, an image view doesn't necessarily have an intrinsic size based on current content since it's designed to scale the image in several ways, independent of the size of the view.

If your goal is to resize the image view to fit the image, perhaps with some limits such as image width less than the screen width, then I think your best bet is to create IBOutlets for the image view's width and height constraints and then adjust them based on your requirements whenever you set the image.


On Jul 21, 2018, at 2:37 PM, Rick Aurbach via Groups.Io <rlaurb@...> wrote:

I have explicit constraints. I expected consistent behavior and really don't understand why I don't get it.

I've tried two cases:
(1) UIImageView directly embedded in a (sub)-controller's view. Fully bound.
(2) UIScrollView directly embedded in a (sub)-controller's view. Fully bound. UIImageView inside the UIScrollView with all four sides bound to the scrollView and with "equal heights" and "equal widths" constraints set.

Both cases behave the same (test case is iPad Air 2 running 11.4.1.


On Jul 20, 2018, at 10:39 PM, Steve Christensen via Groups.Io <punster@...> wrote:

I think I ran into similar issues in the dim past. If I recall correctly, the image view would resize to accommodate the image size if the image was set before a layout pass, or maintain some fixed size (initial frame?) if the image was set later. And I believe this was happening when there were no explicit constraints. 

I didn’t ask before, but do you have constraints, either fixed or proportional to something else, set on the image view? If so, and the image view is set to aspect fit, then you should get consistent behavior no matter the image size. 

Steve


On Jul 20, 2018, at 10:07 PM, Rick Aurbach via Groups.Io <rlaurb@...> wrote:

1. For any image which has an intrinsic size larger than the destination UIImageView object, the sizing behavior is the same. Namely, when the picker is dismissed (calling loadData(), the upper left corner of the image appears, fully expanded. In all other cases where the image is displayed (such as after switching between scenes, etc.), the image has been scaled to fit the UIImageView's physical size.

2. This is two questions. The images that cause problems have a size (i.e. UIImage::size) larger than the physical dimensions of the device. And in both the "correct" and "incorrect" cases, the UIImageView:bounds rectangle has a size equal to the image's size.

Rick

Join {cocoa@apple-dev.groups.io to automatically receive all group messages.