UIImageView with auto layout and maximum aspect ratio

I have UIImageViewgenerated in the source (programmatically). For there UIImageViewmust be a maximum size both in width and in height. In this view, I download the image from the URL and install it after the download is complete. I do not know earlier what the image size is, but I need to keep the aspect ratio and make it UIImageViewwith a size that supports this aspect.

If the image is smaller in one or both dimensions, I want it to UIImageViewbe the size needed to maintain the aspect ratio without stretching the image.

I will give two examples considering the maximum size for UIImageViewc 250 X 350.

Example 1:

Image size 800 X 800. Size UIImageViewshould be 250 X 250.

Example 2:

Image size 200 X 250. Size UIImageViewshould be 200 X 250.

How to do this using auto-layout.

+4
source share
1 answer

You will need three limitations here. One that maintains aspect ratio, one that ensures that the width is not greater than the maximum, and one that ensures that the height is not greater than the maximum. As of August 2016, Interface Builder does not support constraints that reference other constraints, so - at a minimum - aspect ratio constraints must be implemented in code.

- , . V_w V_h - . I_w I_h - . , , :

V_w / V_h = I_w / I_h

, :

V_w = (I_w / I_h) * V_h

, . . , MAX_HEIGHT MAX_WIDTH, . , iOS 9.0 . iOS, . .

// Constrain the desired aspect ratio
[imageView.widthAnchor constraintEqualToAnchor:imageView.heightAnchor
                                    multiplier:aspectRatioMult].active = YES;

// Constrain height
[imageView.heightAnchor constraintLessThanOrEqualToConstant:MAX_HEIGHT].active = YES;

// Constrain width
[imageView.widthAnchor constraintLessThanOrEqualToConstant:MAX_WIDTH].active = YES;

iOS 9.0 NSLayoutAnchor . NSLayoutConstraint.

CGFloat aspectRatioMult = (imageView.image.size.width / imageView.image.size.height);

// Constrain the desired aspect ratio
[imageView addConstraint:
  [NSLayoutConstraint constraintWithItem:imageView
                               attribute:NSLayoutAttributeWidth
                               relatedBy:NSLayoutRelationEqual
                                  toItem:imageView
                               attribute:NSLayoutAttributeHeight
                              multiplier:aspectRatioMult
                                constant:0]];

// Constrain height
[imageView addConstraints:
  [NSLayoutConstraint constraintsWithVisualFormat:@"V:[imageView(<=max)]"
                                          options:0
                                          metrics:@{@"max" : @(MAX_HEIGHT)}
                                            views:@{@"imageView" : imageView}]];

// Constrain width
[imageView addConstraints:
  [NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView(<=max)]"
                                          options:0
                                          metrics:@{@"max" : @(MAX_WIDTH)}
                                            views:@{@"imageView" : imageView}]];
+5

All Articles