Create a gist now

Instantly share code, notes, and snippets.

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;
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