UIImage+Scale

Scale a UIImage to any given rect keeping the aspect ratio
  @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
 

In Swift:

/*
    Image Resizing Techniques: http://bit.ly/1Hv0T6i
*/
class func scaleUIImageToSize(let image: UIImage, let size: CGSize) -> UIImage {
    let hasAlpha = false
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
    image.drawInRect(CGRect(origin: CGPointZero, size: size))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return scaledImage
}
@Bodacious
 

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
 

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.

原文地址:https://www.cnblogs.com/Jenaral/p/5546446.html