swift UI图像大小调整比例

n6lpvg4x  于 2023-01-16  发布在  Swift
关注(0)|答案(3)|浏览(421)

我有这样一个形象:

我想把这张图片调整到特定的宽度:比如200 px高度也应该被计算。(所以图像应该保持它的宽度到高度的比例)
这就是我目前所知道的:

extension UIImage {
    func resize(newSize1: CGSize) -> UIImage {
        let size = self.size
        let widthRatio  = newSize1.width  / size.width
        let heightRatio = newSize1.height / size.height

        var newSize2: CGSize
        if(widthRatio > heightRatio) {
            newSize2 = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
        } else {
            newSize2 = CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)
        }
        let rect = CGRect(x: 0, y: 0, width: newSize2.width, height: newSize2.height)
        UIGraphicsBeginImageContextWithOptions(newSize2, false, 1.0)
        self.draw(in: rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage2!
    }
}

但是这个函数需要一个CGSize大小参数(宽度和高度),但是我只想得到一个接受宽度参数的函数。
如何编辑我的代码来解决我的问题?
任何帮助都将不胜感激

注意:UIImage应获得较小的分辨率(较少的像素),而不是Image View本身!

kknvjkwl

kknvjkwl1#

对于Swift 4和Swift 3,使用下面的UIImage扩展来调整图像大小。它是根据给定的宽度计算的高度。

extension UIImage {
    func resized(toWidth width: CGFloat) -> UIImage? {
        let canvasSize = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
        UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale)
        defer { UIGraphicsEndImageContext() }
        draw(in: CGRect(origin: .zero, size: canvasSize))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}
8wtpewkr

8wtpewkr2#

你可以根据图片的宽度和长宽比来计算想要的高度,同样的方法也可以应用到height参数中,下面是UIImage扩展的一个例子。

extension UIImage {
func resize(width: CGFloat) -> UIImage {
    let height = (width/self.size.width)*self.size.height
    return self.resize(size: CGSize(width: width, height: height))
}

func resize(height: CGFloat) -> UIImage {
    let width = (height/self.size.height)*self.size.width
    return self.resize(size: CGSize(width: width, height: height))
}

func resize(size: CGSize) -> UIImage {
    let widthRatio  = size.width/self.size.width
    let heightRatio = size.height/self.size.height
    var updateSize = size
    if(widthRatio > heightRatio) {
        updateSize = CGSize(width:self.size.width*heightRatio, height:self.size.height*heightRatio)
    } else if heightRatio > widthRatio {
        updateSize = CGSize(width:self.size.width*widthRatio,  height:self.size.height*widthRatio)
    }
    UIGraphicsBeginImageContextWithOptions(updateSize, false, UIScreen.main.scale)
    self.draw(in: CGRect(origin: .zero, size: updateSize))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
}

}

t40tm48m

t40tm48m3#

基于@deoKasuhal的解决方案,我开发了以下方法来缩放图像,以便较大的尺寸(宽度或高度)不超过由参数'base'定义的最大尺寸。如果原始大小为width = 4000,height = 3000,参数'base'设置为200,则返回的图像大小为200 x 150(如果height大于width,则反之亦然):

extension UIImage {
func resize(toBase base: CGFloat) -> UIImage {
    guard base > 0 else {
        return self
    }
    let widthRatio  = base / max(self.size.width, 1)
    let heightRatio = base / max(self.size.height, 1)
    var updateSize = CGSize()
    
    if (widthRatio > heightRatio) {
        updateSize = CGSize(width:self.size.width * heightRatio, height:self.size.height * heightRatio)
    } else {
        updateSize = CGSize(width:self.size.width * widthRatio,  height:self.size.height * widthRatio)
    }
    UIGraphicsBeginImageContextWithOptions(updateSize, false, UIScreen.main.scale)
    self.draw(in: CGRect(origin: .zero, size: updateSize))
    if let newImage = UIGraphicsGetImageFromCurrentImageContext() {
        UIGraphicsEndImageContext()
        return newImage
    } else {
        UIGraphicsEndImageContext()
        return self
    }
}

}

相关问题