Skip to content

Instantly share code, notes, and snippets.

@kylehowells
Last active January 12, 2021 12:34
Show Gist options
  • Select an option

  • Save kylehowells/e5f9198e723018373a1d8a27952011f5 to your computer and use it in GitHub Desktop.

Select an option

Save kylehowells/e5f9198e723018373a1d8a27952011f5 to your computer and use it in GitHub Desktop.
extension UIColor {
func kh_renderImage() -> UIImage? {
let size = CGSize(width:1, height: 1)
return UIGraphicsImageRenderer(size: size).image(actions: { (rendererContext) in
let context = UIGraphicsGetCurrentContext()
self.setFill()
context?.fill(CGRect(origin: .zero, size: size))
})
}
}
// Usage:
//button.setBackgroundImage(UIColor.blue.kh_renderImage(), for: UIControl.State.normal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment