Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Vladimir Zhelnov - neatek.pw - Web/iOS dev
- 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
- 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)
- }
- let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
- UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
- image.draw(in: rect)
- let newImage = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- return newImage!
- }
- // usage: let littleImage = ResizeImage(image: chosenImage, targetSize: CGSize(width: 800, height: 500))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement