Objective-C:
-(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView
{
float imageRatio = image.size.width / image.size.height;
float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
if(imageRatio < viewRatio)
{
float scale = imageView.frame.size.height / image.size.height;
float width = scale * image.size.width;
float topLeftX = (imageView.frame.size.width - width) * 0.5;
return CGRectMake(topLeftX, 0, width, imageView.frame.size.height);
}
else
{
float scale = imageView.frame.size.width / image.size.width;
float height = scale * image.size.height;
float topLeftY = (imageView.frame.size.height - height) * 0.5;
return CGRectMake(0, topLeftY, imageView.frame.size.width, height);
}
}
Swift 4:
func frame(for image: UIImage, inImageViewAspectFit imageView: UIImageView) -> CGRect {
let imageRatio = (image.size.width / image.size.height)
let viewRatio = imageView.frame.size.width / imageView.frame.size.height
if imageRatio < viewRatio {
let scale = imageView.frame.size.height / image.size.height
let width = scale * image.size.width
let topLeftX = (imageView.frame.size.width - width) * 0.5
return CGRect(x: topLeftX, y: 0, width: width, height: imageView.frame.size.height)
} else {
let scale = imageView.frame.size.width / image.size.width
let height = scale * image.size.height
let topLeftY = (imageView.frame.size.height - height) * 0.5
return CGRect(x: 0.0, y: topLeftY, width: imageView.frame.size.width, height: height)
}
}