UIImage+Scale

图片缩放保持比例
本文介绍了一种在不同编程语言中实现的图片缩放方法,该方法能够在保持原始图片长宽比的同时将其缩放到指定尺寸内。适用于Swift、Objective-C及RubyMotion等语言。
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
 
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
 
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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值