Skip to content

Instantly share code, notes, and snippets.

@zummenix
Created August 8, 2016 07:22
Show Gist options
  • Save zummenix/67adccc1ee893ade8fb27a5a5a790498 to your computer and use it in GitHub Desktop.
Save zummenix/67adccc1ee893ade8fb27a5a5a790498 to your computer and use it in GitHub Desktop.
Generates an image with radial gradient.
private extension UIImage {
static func radialGradientImageWithSize(size: CGSize, outerColor: UIColor, innerColor: UIColor) -> UIImage {
let colorSpace = CGColorSpaceCreateDeviceRGB()
let gradient = CGGradientCreateWithColors(colorSpace, [outerColor.CGColor, innerColor.CGColor], [1.0, 0.0])
let center = CGPoint(x: size.width / 2.0, y: size.height / 2.0)
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale)
let imageContext = UIGraphicsGetCurrentContext()
CGContextDrawRadialGradient(imageContext, gradient, center, 0.0, center, size.width / 2.0, CGGradientDrawingOptions.DrawsAfterEndLocation)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment