Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if image.width > image.height {
- imageView.contentMode = UIViewContentModeScaleAspectFit
- //since the width > height we may fit it and we'll have bands on top/bottom
- } else {
- imageView.contentMode = UIViewContentModeScaleAspectFill
- //width < height we fill it until width is taken up and clipped on top/bottom
- }
- private func updateUI() {
- guard let image = image else { return }
- let viewAspectRatio = self.bounds.width / self.bounds.height
- let imageAspectRatio = image.size.width / image.size.height
- if viewAspectRatio > imageAspectRatio {
- self.contentMode = .scaleAspectFill
- } else {
- self.contentMode = .scaleAspectFit
- }
- }
- override var image: UIImage? { didSet { updateUI() }}
- override func layoutSubviews() {
- super.layoutSubviews()
- updateUI()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement