Skip to content

Instantly share code, notes, and snippets.

@phucnm
Created June 7, 2016 03:47
Show Gist options
  • Save phucnm/79fc6c29cb6d30f357340cc1718c1b27 to your computer and use it in GitHub Desktop.
Save phucnm/79fc6c29cb6d30f357340cc1718c1b27 to your computer and use it in GitHub Desktop.
UIImage with gradient color
extension UIImage {
/**
Create gradient image from beginColor on top and end color at bottom
- parameter beginColor: beginColor
- parameter endColor: endColor
- parameter frame: frame to be filled
- returns: filled image
*/
static func imageWithGradient(from beginColor: UIColor, to endColor: UIColor, with frame: CGRect) -> UIImage {
let layer = CAGradientLayer()
layer.frame = frame
layer.colors = [beginColor.CGColor, endColor.CGColor]
UIGraphicsBeginImageContext(CGSize(width: frame.width, height: frame.height))
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment