Skip to content

Instantly share code, notes, and snippets.

@joshuajhomann
Last active April 6, 2019 19:42
Show Gist options
  • Save joshuajhomann/25f87d9d6b29d7f5bfb8d334eea3f7da to your computer and use it in GitHub Desktop.
Save joshuajhomann/25f87d9d6b29d7f5bfb8d334eea3f7da to your computer and use it in GitHub Desktop.
import UIKit
extension UIImage {
func resize(to size: CGSize) -> UIImage {
return UIGraphicsImageRenderer(size: size).image { context in
self.draw(in: CGRect(origin: .zero, size: size))
}
}
func resizeAndCrop(to square: CGFloat)-> UIImage {
let targetSize = CGSize(width: square, height: square)
return UIGraphicsImageRenderer(size: targetSize).image { context in
if self.size.width > self.size.height {
let offset = (self.size.width - self.size.height) / 2
self.draw(in: CGRect(origin: CGPoint(x: -offset, y: 0), size: size))
} else {
let offset = (self.size.height - self.size.width) / 2
self.draw(in: CGRect(origin: CGPoint(x: 0, y: -offset), size: size))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment