Last active
April 13, 2016 03:13
-
-
Save jaisonv/2a3db17075235454c7185ff1877b0890 to your computer and use it in GitHub Desktop.
Scale image based in a width
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
| -(UIImage *)scaleImage:(UIImage *)image toWidth:(int)width { | |
| // determine the scaling factor to fit the screen width | |
| CGFloat oldWidth = image.size.width; | |
| NSUInteger newWidth = (width - 10); | |
| CGFloat scaleFactor; | |
| if (oldWidth > newWidth) | |
| scaleFactor = oldWidth / newWidth; | |
| else | |
| scaleFactor = 1; | |
| // create scaled version of image | |
| CGImageRef imageRef = image.CGImage; | |
| UIImage *scaledImage = [UIImage imageWithCGImage:imageRef scale:scaleFactor orientation:UIImageOrientationUp]; | |
| return scaledImage; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment