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

Posted

技术标签:

【中文标题】调整图像大小而不扭曲或裁剪它【英文标题】:Resize Image without distorting or cropping it 【发布时间】:2017-06-07 15:24:19 【问题描述】:

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

我尝试使用此图像处理器,但它没有达到我想要的效果。 https://github.com/gavinbunney/Toucan。它只裁剪图像。

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

【问题讨论】:

您真的要修改图像本身吗?还是只是改变它的显示方式? 我们需要修改图片本身。不仅仅是改变它的显示方式。我们正在将其上传到我们的服务器,并且需要以修改后的格式上传。 gist.github.com/tomasbasham/10533743 - 向下滚动到评论标题:Oyvindkg commented on Feb 4 成功了!非常感谢 【参考方案1】:

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

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

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

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)


【讨论】:

以上是关于调整图像大小而不扭曲或裁剪它的主要内容,如果未能解决你的问题,请参考以下文章

调整图像大小而不扭曲比例(JPEGGIF或PNG)

调整图像大小并裁剪为新的纵横比

如何在网站中动态裁剪/缩放图像

调整位图的大小,保持宽高比,不会出现失真和裁剪

允许在 PHP 中裁剪和调整图像大小而不使用太多内存的图像功能 [关闭]

调整图像大小并显示而不保存