Skip to content

Instantly share code, notes, and snippets.

@cwagdev
Created August 17, 2015 23:18
Show Gist options
  • Save cwagdev/2807e8397a37c100856b to your computer and use it in GitHub Desktop.
Save cwagdev/2807e8397a37c100856b to your computer and use it in GitHub Desktop.
UIImage?(color: UIColor) initializer
extension UIImage {
convenience init!(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.init(CGImage: image.CGImage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment