Skip to content

Instantly share code, notes, and snippets.

@tewha
Last active December 15, 2015 13:18
Show Gist options
  • Save tewha/5265978 to your computer and use it in GitHub Desktop.
Save tewha/5265978 to your computer and use it in GitHub Desktop.
This is a category on UIImage.
- (UIImage *)rxScalePropertionallyToMaximumSize:(CGSize)maxSize blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha scale:(CGFloat)scale {
if ((maxSize.width < 1.0e-15) || (maxSize.height < 1.0e-15)) return nil;
CGSize imageSize = self.size;
CGSize scaleFactors = {
.width = maxSize.width / imageSize.width,
.height = maxSize.height / imageSize.height,
};
CGFloat scaleFactor = MIN(scaleFactors.width, scaleFactors.height);
CGSize newSize = {
.width = imageSize.width * scaleFactor,
.height = imageSize.height * scaleFactor
};
CGRect imageFrame = (CGRect){
.origin = CGPointZero,
.size = newSize
};
UIGraphicsBeginImageContextWithOptions(newSize, YES, scale);
[self drawInRect:imageFrame blendMode:blendMode alpha:alpha];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@tewha
Copy link
Author

tewha commented Mar 28, 2013

This is a category on UIImage. It's intended to take a photo from the camera, image library or camera roll and shrink it. (The input size is smaller than the original size.)

But I've been having a lot of problems getting it to work with all orientations and sources.

@tewha
Copy link
Author

tewha commented Mar 28, 2013

I think this version will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment