Last active
December 15, 2015 13:18
-
-
Save tewha/5265978 to your computer and use it in GitHub Desktop.
This is a category on UIImage.
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 *)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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this version will work.