Last active
March 2, 2017 07:41
-
-
Save minikin/bd603d62109ead8974f3f4bfcf705901 to your computer and use it in GitHub Desktop.
UIImage Extensions
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
extension UIImage { | |
func resizeImage(_ image: UIImage, newWidth: CGFloat) -> UIImage? { | |
let scale = newWidth / image.size.width | |
let newHeight = image.size.height * scale | |
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight)) | |
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight)) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
guard let image = newImage else { return nil } | |
UIGraphicsEndImageContext() | |
return image | |
} | |
func drawFromView(view: UIView) -> UIImage? { | |
UIGraphicsBeginImageContext(view.frame.size) | |
view.layer.render(in: UIGraphicsGetCurrentContext()!) | |
let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
guard let image = newImage else { return nil } | |
UIGraphicsEndImageContext() | |
return image | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment