Last active
April 6, 2019 19:42
-
-
Save joshuajhomann/25f87d9d6b29d7f5bfb8d334eea3f7da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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