I have some UIImage
and display it in UIImageView
.
I need to use UIViewContentModeScaleAspectFit
content mode.
Here i
In swift, same as @jacob-relkin
extension UIImageView {
func displayedImageBounds() -> CGRect {
let boundsWidth = bounds.size.width
let boundsHeight = bounds.size.height
let imageSize = image!.size
let imageRatio = imageSize.width / imageSize.height
let viewRatio = boundsWidth / boundsHeight
if ( viewRatio > imageRatio ) {
let scale = boundsHeight / imageSize.height
let width = scale * imageSize.width
let topLeftX = (boundsWidth - width) * 0.5
return CGRectMake(topLeftX, 0, width, boundsHeight)
}
let scale = boundsWidth / imageSize.width
let height = scale * imageSize.height
let topLeftY = (boundsHeight - height) * 0.5
return CGRectMake(0,topLeftY, boundsWidth,height)
}
}