Skip to content

Instantly share code, notes, and snippets.

@jaisonv jaisonv/ImageScaled.m

Last active Apr 13, 2016
What would you like to do?
Scale image based in a width
-(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;
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
You can’t perform that action at this time.