Skip to content

Instantly share code, notes, and snippets.

@couchoud
Created March 11, 2010 18:12
Show Gist options
  • Save couchoud/329466 to your computer and use it in GitHub Desktop.
Save couchoud/329466 to your computer and use it in GitHub Desktop.
+ (CGRect) aspectFittedRect:(CGRect)inRect max:(CGRect)maxRect
{
float originalAspectRatio = inRect.size.width / inRect.size.height;
float maxAspectRatio = maxRect.size.width / maxRect.size.height;
CGRect newRect = maxRect;
if (originalAspectRatio > maxAspectRatio) { // scale by width
newRect.size.height = maxRect.size.height * inRect.size.height / inRect.size.width;
newRect.origin.y += (maxRect.size.height - newRect.size.height)/2.0;
} else {
newRect.size.width = maxRect.size.height * inRect.size.width / inRect.size.height;
newRect.origin.x += (maxRect.size.width - newRect.size.width)/2.0;
}
return CGRectIntegral(newRect);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment