Skip to content

Instantly share code, notes, and snippets.

@BenBarahona
Created August 21, 2014 22:32
Show Gist options
  • Save BenBarahona/8fef4b0f1bb48a285186 to your computer and use it in GitHub Desktop.
Save BenBarahona/8fef4b0f1bb48a285186 to your computer and use it in GitHub Desktop.
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize
cropImage:(BOOL)crop {
// Create a graphics image context
CGFloat wFactor = newSize.width / image.size.width;
CGFloat hFactor = newSize.height / image.size.height;
CGFloat maxFactor = MAX(wFactor, hFactor);
if (crop) {
UIGraphicsBeginImageContext(newSize);
} else {
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * maxFactor, image.size.height * maxFactor));
}
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,image.size.width * maxFactor,image.size.height * maxFactor)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment