Created
August 21, 2014 22:32
-
-
Save BenBarahona/8fef4b0f1bb48a285186 to your computer and use it in GitHub Desktop.
This file contains 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*)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