Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Created July 17, 2018 20:58
Show Gist options
  • Select an option

  • Save jakehawken/63c5020eb6b4c50302cd3a411c3aa494 to your computer and use it in GitHub Desktop.

Select an option

Save jakehawken/63c5020eb6b4c50302cd3a411c3aa494 to your computer and use it in GitHub Desktop.
Quickly generate images of programmatic colors on the fly.
extension UIImageView
func setImage(withColor color: UIColor) {
image = UIImage.withColor(color, size: bounds.size)
}
}
extension UIImage {
static func withColor(_ color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(origin: CGPoint.zero, size: size)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment