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

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

概述用户上传任意大小的图像,我们需要调整大小,使其变为正方形而不会扭曲裁剪图像.基本上,它应该在图像视图中执行类似于“Aspect Fit”内容模式的 *** 作.因此,如果我们有一个200x100px的png图像,我想使它成为200x200px并且在高度上额外的100px是透明空间.它不应该将图像裁剪为200×200. 我试图使用这个图像处理器,但它没有做我想要的. https://github.com/ 用户上传任意大小的图像,我们需要调整大小,使其变为正方形而不会扭曲或裁剪图像.基本上,它应该在图像视图中执行类似于“Aspect Fit”内容模式的 *** 作.因此,如果我们有一个200x100px的png图像,我想使它成为200x200px并且在高度上额外的100px是透明空间.它不应该将图像裁剪为200×200.

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

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

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

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

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

import UIKitimport PlaygroundSupportlet container = UIVIEw(frame: CGRect(x: 0,y: 0,wIDth: 800,height: 800))container.backgroundcolor = UIcolor.bluePlaygroundPage.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 – 调整图像大小而不扭曲或裁剪它全部内容,希望文章能够帮你解决ios – 调整图像大小而不扭曲或裁剪它所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1001442.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存