Huy Pham Huy Pham - 2 years ago 241
iOS Question

Create UIImage with solid color in Swift

I want to programmatically create an UIImage filled by a solid color. Anyone have an idea of how to do this in Swift?

Answer Source

Here's another option. I believe you wanted an exact UIImage object.

func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
    let rect = CGRectMake(0, 0, size.width, size.height)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    return image

Stick this in your Swift code and call it

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download