Created
March 28, 2016 16:45
-
-
Save isoiphone/031da3656d69c0d85805 to your computer and use it in GitHub Desktop.
Create UIImage of solid color
This file contains hidden or 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 { | |
class func imageWithColor(color: UIColor, size: CGSize=CGSize(width: 1, height: 1)) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(size, false, 0) | |
color.setFill() | |
UIRectFill(CGRect(origin: CGPoint.zero, size: size)) | |
let image = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return image | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UIGraphicsGetImageFromCurrentImageContext
return optional value, so it should have optional when return:func imageWithColor(color: UIColor, size: CGSize=CGSize(width: 1, height: 1)) -> UIImage?