Created
June 7, 2016 03:47
-
-
Save phucnm/79fc6c29cb6d30f357340cc1718c1b27 to your computer and use it in GitHub Desktop.
UIImage with gradient color
This file contains hidden or 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 { | |
/** | |
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