import UIKit
extension UIImage {
/// 이미지 사이즈 조정
func resizeImage(size: CGSize) -> UIImage {
let originalSize = self.size
let ratio: CGFloat = {
return originalSize.width > originalSize.height ? 1 / (size.width / originalSize.width) :
1 / (size.height / originalSize.height)
}()
return UIImage(cgImage: self.cgImage!, scale: self.scale * ratio, orientation: self.imageOrientation)
}
/// 이미지 사이즈 배율에 맞게 resize
/*
사용방법 : let img = image.resize(newWidth: 1024)
*/
func resize(newWidth: CGFloat) -> UIImage {
let scale = newWidth / self.size.width
let newHeight = self.size.height * scale
let size = CGSize(width: newWidth, height: newHeight)
let render = UIGraphicsImageRenderer(size: size)
let renderImage = render.image { context in
self.draw(in: CGRect(origin: .zero, size: size))
}
print("화면 배율: \(UIScreen.main.scale)")
// 배수
print("origin: \(self), resize: \(renderImage)")
return renderImage
}
}