Xcode Imageview Aspect Fit Remove Extra Space

I have an image in a UITableViewCell.

Imageview with Aspect Fit creates a lot of extra space at the top and bottom if the image is large. Additional space (background gray) is shown in the figure below.

How to remove excess space?

Imageview Extra Space
A few notes:

  • This image is 890 x 589 and the picture was taken on the iphone 6S simulator.

  • I use UITableViewAutomaticDimension to automatically height the table cell.

  • There is no height limit in the image view, so it can resize accordingly.

Thanks in advance.

+4
source share
1 answer

. , ( / ), UIImageView, , Aspect Fit, UIImageView.

, UIImageView, Aspect Fit , . ( - ), , () 600 (h) x 800 (w) ( 3: 4). UIImageView (, 320), 240 (h) x 320 (w) ( 3: 4). 600 x 800, UIImageView , UIImageView 600 x 320 → UIImageView (600), .

: (Swift 3)

UIImageView Main.storyboard Outlet:

@IBOutlet weak var displayimage: UIImageView!
@IBOutlet weak var displayimageHeightConstraint: NSLayoutConstraint!

, UIImageView (), , . , , UIImageView , :

if displayimage.frame.size.width < (displayimage.image?.size.width)! {
    displayimageHeightConstraint.constant = displayimage.frame.size.width / (displayimage.image?.size.width)! * (displayimage.image?.size.height)!
}

, - , UIImageView , ​​ .

@IBOutlet weak var displayimageWidthConstraint: NSLayoutConstraint!

if displayimage.frame.size.height < (displayimage.image?.size.height)! {
    displayimageWidthConstraint.constant = displayimage.frame.size.height / (displayimage.image?.size.height)! * (displayimage.image?.size.height)!
}
+2

Source: https://habr.com/ru/post/1648482/


All Articles