不扭曲或裁剪的情况下调整图像大小

4
用户上传任何尺寸的图像,我们需要将其调整大小为正方形,而不失真或裁剪图像。基本上,它应该执行与图像视图中的“Aspect Fit”内容模式类似的操作。因此,如果我们有一个200x100px的png图像,我想将其变成200x200px,并使高度中多余的100px为透明空间。它不应该将图像剪切为200x200。
我尝试使用此图像处理器,但它无法满足我的需求。 https://github.com/gavinbunney/Toucan 。它只会剪裁图像。
用Swift如何完成这个任务?是否有比我上面提到的框架更好的框架可以更轻松地完成此操作呢?基本上,我正在寻找最简单的方法来完成这个任务。

你真的想修改图像本身吗?还是只想改变它的显示方式? - DonMag
我们需要修改图像本身,而不仅仅是改变它的显示方式。我们将其上传到服务器上,并且需要以修改后的格式上传。 - Nevin Jethmalani
1
Oyvindkg在2月4日发表了评论:我认为这个库非常有用,但是我遇到了一个问题。当我使用-f标志运行脚本时,它会在终端中输出结果,但是当我将输出重定向到文件时,它不会输出任何内容。我该如何解决这个问题? - DonMag
那个有效了!非常感谢。 - Nevin Jethmalani
1个回答

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)

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接