Skip to content

Instantly share code, notes, and snippets.

@VuMai
Last active August 29, 2015 14:22
Show Gist options
  • Save VuMai/cca4a3fe96beda723d42 to your computer and use it in GitHub Desktop.
Save VuMai/cca4a3fe96beda723d42 to your computer and use it in GitHub Desktop.
How to get the size of a scaled UIImage in UIImageView?
-(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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment