Last active
August 29, 2015 14:22
-
-
Save VuMai/cca4a3fe96beda723d42 to your computer and use it in GitHub Desktop.
How to get the size of a scaled UIImage in UIImageView?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -(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