调整图像大小而不扭曲或裁剪它

时间:2017-06-07 15:24:19

标签: ios swift image-processing

用户上传任意大小的图像,我们需要调整它的大小,使其变为正方形而不会扭曲或裁剪图像。基本上,它应该做一些类似于" Aspect Fit"图像视图中的内容模式。因此,如果我们有一个200x100px的png图像,我想使它成为200x200px并且在高度上额外的100px是透明空间。它不应该将图像裁剪为200x200。

我试图使用这个图像处理器,但它没有做我想要的。 https://github.com/gavinbunney/Toucan。它只会裁剪图像。

我如何在swift中做到这一点,是否有一个比我上面提到的更好的框架,使这更容易。基本上,我正在寻找最简单的方法来做到这一点。

1 个答案:

答案 0 :(得分:0)

将此作为答案发布,以及示例用法......

缩放代码不是我的,它来自:https://gist.github.com/tomasbasham/10533743#gistcomment-1988471

以下是您可以在游乐场中运行以进行测试的代码:

import UIKit
import PlaygroundSupport

let container = UIView(frame: CGRect(x: 0, y: 0, width: 800, height: 800))

container.backgroundColor = UIColor.blue

PlaygroundPage.current.liveView = container


// MARK: - Image Scaling.
extension UIImage {

    /// Represents a scaling mode
    enum ScalingMode {
        case aspectFill
        case aspectFit

        /// Calculates the aspect ratio between two sizes
        ///
        /// - parameters:
        ///     - size:      the first size used to calculate the ratio
        ///     - otherSize: the second size used to calculate the ratio
        ///
        /// - return: the aspect ratio between the two sizes
        func aspectRatio(between size: CGSize, and otherSize: CGSize) -> CGFloat {
            let aspectWidth  = size.width/otherSize.width
            let aspectHeight = size.height/otherSize.height

            switch self {
            case .aspectFill:
                return max(aspectWidth, aspectHeight)
            case .aspectFit:
                return min(aspectWidth, aspectHeight)
            }
        }
    }

    /// Scales an image to fit within a bounds with a size governed by the passed size. Also keeps the aspect ratio.
    ///
    /// - parameter:
    ///     - newSize:     the size of the bounds the image must fit within.
    ///     - scalingMode: the desired scaling mode
    ///
    /// - returns: a new scaled image.
    func scaled(to newSize: CGSize, scalingMode: UIImage.ScalingMode = .aspectFill) -> UIImage {

        let aspectRatio = scalingMode.aspectRatio(between: newSize, and: size)

        /* Build the rectangle representing the area to be drawn */
        var scaledImageRect = CGRect.zero

        scaledImageRect.size.width  = size.width * aspectRatio
        scaledImageRect.size.height = size.height * aspectRatio
        scaledImageRect.origin.x    = (newSize.width - size.width * aspectRatio) / 2.0
        scaledImageRect.origin.y    = (newSize.height - size.height * aspectRatio) / 2.0

        /* Draw and retrieve the scaled image */
        UIGraphicsBeginImageContext(newSize)

        draw(in: scaledImageRect)
        let scaledImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        return scaledImage!
    }
}

if let srcimg = UIImage(named: "flags") {

    let w = srcimg.size.width
    let h = srcimg.size.height

    // determine whether width or height is greater
    let longer = max(w, h)

    // create a Square size
    let sz = CGSize(width: longer, height: longer)

    // call scaling function to scale the image to the Square dimensions,
    // using "aspect fit"
    let newImage = srcimg.scaled(to: sz, scalingMode: .aspectFit)

    // create a UIImageView with the resulting image
    let v = UIImageView(image: newImage)
    v.backgroundColor = UIColor.white

    // add it to the container view
    container.addSubview(v)

}