Skip to content

Instantly share code, notes, and snippets.

@efremidze
Created January 30, 2017 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efremidze/1cabc32875fb44b3773047e698ad7d5d to your computer and use it in GitHub Desktop.
Save efremidze/1cabc32875fb44b3773047e698ad7d5d to your computer and use it in GitHub Desktop.
import UIKit
import AVFoundation
class ResizingImageView: UIImageView {
private var heightConstraint: NSLayoutConstraint?
override var image: UIImage? {
didSet {
_ = heightConstraint.map { NSLayoutConstraint.deactivate([$0]) }
var boundingRect = self.bounds
boundingRect.size.height = .greatestFiniteMagnitude
let rect = AVMakeRect(aspectRatio: image?.size ?? CGSize(), insideRect: boundingRect)
heightConstraint = self.heightAnchor.constraint(equalToConstant: rect.height)
_ = heightConstraint.map { NSLayoutConstraint.activate([$0]) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment