@implementation UIImage (scale) | |
/** | |
* Scales an image to fit within a bounds with a size governed by | |
* the passed size. Also keeps the aspect ratio. | |
* | |
* Switch MIN to MAX for aspect fill instead of fit. | |
* | |
* @param newSize the size of the bounds the image must fit within. | |
* @return a new scaled image. | |
*/ | |
- (UIImage *)scaleImageToSize:(CGSize)newSize { | |
CGRect scaledImageRect = CGRectZero; | |
CGFloat aspectWidth = newSize.width / self.size.width; | |
CGFloat aspectHeight = newSize.height / self.size.height; | |
CGFloat aspectRatio = MIN ( aspectWidth, aspectHeight ); | |
scaledImageRect.size.width = self.size.width * aspectRatio; | |
scaledImageRect.size.height = self.size.height * aspectRatio; | |
scaledImageRect.origin.x = (newSize.width - scaledImageRect.size.width) / 2.0f; | |
scaledImageRect.origin.y = (newSize.height - scaledImageRect.size.height) / 2.0f; | |
UIGraphicsBeginImageContextWithOptions( newSize, NO, 0 ); | |
[self drawInRect:scaledImageRect]; | |
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return scaledImage; | |
} | |
@end |
King-Wizard commented on 27 May 2015
Bodacious commented on 27 Nov 2015
In Rubymotion
class UIImage
# Scales an image to fit within a bounds with a size governed by
# the passed size. Also keeps the aspect ratio.
#
# newSize - the CGSize of the bounds the image must fit within.
# aspect - A Symbol stating the aspect mode (defaults: :min)
#
# Returns a new scaled UIImage
def scaleImageToSize(newSize, aspect = :fit)
scaledImageRect = CGRectZero
aspectRules = { :fill => :max } # else :min
aspectWidth = Rational(newSize.width, size.width)
aspectHeight = Rational(newSize.height, size.height)
aspectRatio = [aspectWidth, aspectHeight].send(aspectRules[aspect] || :min)
scaledImageRect.size = (size.width * aspectRatio).round
scaledImageRect.size.height = (size.height * aspectRatio).round
scaledImageRect.origin.x = Rational(newSize.width - scaledImageRect.size.width, 2.0).round
scaledImageRect.origin.y = Rational(newSize.height - scaledImageRect.size.height, 2.0).round
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
drawInRect(scaledImageRect)
scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
scaledImage
end
end
reinderdevries commented on 8 Apr
In Swift 2 (with keeping aspect ratio)
func imageWithSize(size:CGSize) -> UIImage
{
var scaledImageRect = CGRect.zero;
let aspectWidth:CGFloat = size.width / self.size.width;
let aspectHeight:CGFloat = size.height / self.size.height;
let aspectRatio:CGFloat = min(aspectWidth, aspectHeight);
scaledImageRect.size.width = self.size.width * aspectRatio;
scaledImageRect.size.height = self.size.height * aspectRatio;
scaledImageRect.origin.x = (size.width - scaledImageRect.size.width) / 2.0;
scaledImageRect.origin.y = (size.height - scaledImageRect.size.height) / 2.0;
UIGraphicsBeginImageContextWithOptions(size, false, 0);
self.drawInRect(scaledImageRect);
let scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
If you want "aspect fill", instead of "aspect fit", change function min
to max
.
In Swift: