0
가로 세로 비율을 유지하여 이미지 크기를 조정하려고합니다. 그러나 크기가 조정 된 이미지를 둘러싼 흰색 공백이 있습니다. 나는 내가 잘못 무엇공백 크기 조정 된 이미지
extension NSImage {
func resizeTo(width: CGFloat, height: CGFloat) -> NSImage {
let ratioX = width/size.width
let ratioY = height/size.height
let ratio = ratioX < ratioY ? ratioX : ratioY
let newHeight = size.height * ratio
let newWidth = size.width * ratio
let canvasSize = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
let img = NSImage(size: canvasSize)
img.lockFocus()
let context = NSGraphicsContext.current()
context?.imageInterpolation = .high
draw(in: NSRect(origin: .zero, size: NSSize(width: newWidth,height: newHeight)), from: NSRect(origin: .zero, size: size) , operation: .copy, fraction: 1)
img.unlockFocus()
return img
}
}
?
알았어. 어떻게 코드를 개선 할 수 있습니까? – techno
이미지의 크기를 늘리지 않기 위해 코드를 향상시키려는 경우 ratio :'if (ratio)> 0 ratio = 1;' – Gedrox
의 체크를 덧붙이면'var ratio = ratioX 0 { 비율 = 1 } –
techno