Skip to content

Instantly share code, notes, and snippets.

@minikin
Last active March 2, 2017 07:41
Show Gist options
  • Save minikin/bd603d62109ead8974f3f4bfcf705901 to your computer and use it in GitHub Desktop.
Save minikin/bd603d62109ead8974f3f4bfcf705901 to your computer and use it in GitHub Desktop.
UIImage Extensions
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