Created
August 17, 2015 23:18
-
-
Save cwagdev/2807e8397a37c100856b to your computer and use it in GitHub Desktop.
UIImage?(color: UIColor) initializer
This file contains 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 { | |
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