Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. if image.width > image.height {
  2. imageView.contentMode = UIViewContentModeScaleAspectFit
  3. //since the width > height we may fit it and we'll have bands on top/bottom
  4. } else {
  5. imageView.contentMode = UIViewContentModeScaleAspectFill
  6. //width < height we fill it until width is taken up and clipped on top/bottom
  7. }
  8.  
  9. private func updateUI() {
  10. guard let image = image else { return }
  11. let viewAspectRatio = self.bounds.width / self.bounds.height
  12. let imageAspectRatio = image.size.width / image.size.height
  13. if viewAspectRatio > imageAspectRatio {
  14. self.contentMode = .scaleAspectFill
  15. } else {
  16. self.contentMode = .scaleAspectFit
  17. }
  18. }
  19.  
  20. override var image: UIImage? { didSet { updateUI() }}
  21.  
  22. override func layoutSubviews() {
  23. super.layoutSubviews()
  24. updateUI()
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement