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

前端之家收集整理的这篇文章主要介绍了ios – 调整图像大小而不扭曲或裁剪它前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
用户上传任意大小的图像,我们需要调整大小,使其变为正方形而不会扭曲或裁剪图像.基本上,它应该在图像视图中执行类似于“Aspect Fit”内容模式的操作.因此,如果我们有一个200x100px的png图像,我想使它成为200x200px并且在高度上额外的100px是透明空间.它不应该将图像裁剪为200×200.

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

我将如何在swift中执行此操作,并且是否有一个比我上面提到的更好的框架来使这更容易.基本上,我正在寻找最简单的方法来做到这一点.

解决方法

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

缩放代码不是我的,它来自: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)

}

猜你在找的iOS相关文章