通过在swift中保留纵横比来调整图像大小

通过在swift中保留纵横比来调整图像大小,swift,macos,Swift,Macos,我使用以下扩展来调整图像的大小 extension NSImage { func resizeImage(width: CGFloat, _ height: CGFloat) -> NSImage { let img = NSImage(size: CGSize(width:width, height:height)) img.lockFocus() let ctx = NSGraphicsContext.current()

我使用以下扩展来调整图像的大小

extension NSImage {
    func resizeImage(width: CGFloat, _ height: CGFloat) -> NSImage {
        let img = NSImage(size: CGSize(width:width, height:height))

        img.lockFocus()
        let ctx = NSGraphicsContext.current()
        ctx?.imageInterpolation = .high
        self.draw(in: NSMakeRect(0, 0, width, height), from: NSMakeRect(0, 0, size.width, size.height), operation: .copy, fraction: 1)
        img.unlockFocus()

        return img
    }
}
调整尺寸时,不保留纵横比

如何修改代码以保持纵横比

请给我一些建议

更新:

这是C#中使用的逻辑。我不知道如何在swift中实现这一点

double ratioX = (double) canvasWidth / (double) originalWidth;
    double ratioY = (double) canvasHeight / (double) originalHeight;
    // use whichever multiplier is smaller
    double ratio = ratioX < ratioY ? ratioX : ratioY;

    // now we can get the new height and width
    int newHeight = Convert.ToInt32(originalHeight * ratio);
    int newWidth = Convert.ToInt32(originalWidth * ratio);

    // Now calculate the X,Y position of the upper-left corner 
    // (one of these will always be zero)
    int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2);
    int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2);
double ratioX=(双)画布宽度/(双)原始宽度;
双倍比率=(双倍)画布高度/(双倍)原始高度;
//使用较小的乘数
双倍比率=比率x
您可以更改方法签名,使其使用百分比而不是大小缩放图像:

extension NSImage {
    func resizedTo(width: CGFloat, height: CGFloat) -> NSImage {
        let ratioX = width / size.width
        let ratioY = height / size.height
        let ratio = ratioX < ratioY ? ratioX : ratioY
        let canvasSize = NSSize(width: size.width * ratio, height: size.height * ratio)
        let img = NSImage(size: canvasSize)
        img.lockFocus()
        NSGraphicsContext.current?.imageInterpolation = .high
        draw(in: NSRect(origin: CGPoint(x: (canvasSize.width - (size.width * ratio)) / 2, y: (canvasSize.height - (size.height * ratio)) / 2), size: canvasSize), from: NSRect(origin: .zero, size: size), operation: .copy, fraction: 1)
        img.unlockFocus()
        return img
    }
    func resizedTo(percentage: CGFloat) -> NSImage {
        let canvasSize = CGSize(width: size.width * percentage, height: size.height * percentage)
        let img = NSImage(size: canvasSize)
        img.lockFocus()
        NSGraphicsContext.current?.imageInterpolation = .high
        draw(in: NSRect(origin: .zero, size: canvasSize), from: NSRect(origin: .zero, size: size), operation: .copy, fraction: 1)
        img.unlockFocus()
        return img
    }
    func resizedTo(width: CGFloat) -> NSImage {
        let canvasSize = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
        let img = NSImage(size: canvasSize)
        img.lockFocus()
        NSGraphicsContext.current?.imageInterpolation = .high
        draw(in: NSRect(origin: .zero, size: canvasSize), from: NSRect(origin: .zero, size: size), operation: .copy, fraction: 1)
        img.unlockFocus()
        return img
    }
}
扩展NSImage{
func resizedTo(宽度:CGFloat,高度:CGFloat)->NSImage{
让ratioX=宽度/尺寸.width
let ratioY=高度/尺寸高度
let ratio=ratioXNSImage{
让canvasSize=CGSize(宽度:size.width*百分比,高度:size.height*百分比)
让img=NSImage(大小:画布大小)
img.lockFocus()
NSGraphicsContext.current?.imageInterpolation=.high
绘制(在:NSRect(原点:.0,大小:canvasSize)中),从:NSRect(原点:.0,大小:大小),操作:.copy,分数:1)
img.unlockFocus()
返回img
}
func resizedTo(宽度:CGFloat)->NSImage{
让canvasSize=CGSize(宽度:宽度,高度:CGFloat(ceil(宽度/大小.宽度*大小.高度)))
让img=NSImage(大小:画布大小)
img.lockFocus()
NSGraphicsContext.current?.imageInterpolation=.high
绘制(在:NSRect(原点:.0,大小:canvasSize)中),从:NSRect(原点:.0,大小:大小),操作:.copy,分数:1)
img.unlockFocus()
返回img
}
}

@LinusGeffarth我对这门语言不熟悉。你能不能把它作为一个答案发布出来。我不能这样做,因为。。。我需要询问用户宽度和高度不能这样做。。。。我只需要在方法内部做所有事情。如果我批量处理图像,我能做什么?但它不要求用户输入百分比值。。。我想我需要一个解决方案,找到一个最小值并计算一个共同的比例因子。你的代码可以工作,但在调整大小的图像周围有空白。