Я сделал расширение, которое позволяет изменять размер UIImage, но мне интересно, правильно ли я его назову. Расширение находится в его собственном файле и как это:UIImage Resize Extension
extension UIImage {
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width/image.size.width
let heightRatio = targetSize.height/image.size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
Я тогда называть это так:
img.resizeImage(image: img, targetSize: CGSize(width: 200.0, height: 200.0))
где IMG является UIImage. Однако мне кажется странным, что я вызываю функцию на UIImage, но также передаю ее в качестве аргумента. Это правильный способ сделать это или есть более сжатый способ?