簡體   English   中英

UIImage 縮放比例

[英]UIImage resizing scale

我有這樣的圖像:

看看這張圖片

我想將此圖像調整為特定寬度:假設 200px也應該計算高度。 (因此圖像應保持其寬高比例)

這是我到目前為止所得到的:

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!
    }
}

但是 function 采用CGSize大小參數(寬度和高度),但我只想獲得采用寬度參數的 function。

如何編輯我的代碼來解決我的問題?
任何幫助將不勝感激:)

注意:UIImage 應該獲得更小的分辨率(更少像素),而不是圖像視圖本身!

您可以根據圖像的寬度和縱橫比計算所需的高度。 高度參數也可以相同。 這是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!
}

}

對於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()
    }
}

基於@deoKasuhal 的解決方案,我開發了以下方法來縮放圖像,以便更大的尺寸(寬度或高度)不超過由參數“base”定義的定義的最大尺寸。 示例:如果原始大小為 width = 4000 和 height = 3000 並且參數“base”設置為 200,則返回的圖像大小為 200 x 150(如果高度大於寬度,反之亦然):

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
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM