Skip to content

Instantly share code, notes, and snippets.

@mingsai
Created April 3, 2018 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingsai/7de00af9d96bd9664066fcf89eb4a7ca to your computer and use it in GitHub Desktop.
Save mingsai/7de00af9d96bd9664066fcf89eb4a7ca to your computer and use it in GitHub Desktop.
import UIKit
extension UIImage {
class func colorAsImage(from color:UIColor, for size:CGSize) -> UIImage? {
var img:UIImage?
let rect = CGRect(x:0.0, y:0.0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
context.setFillColor(color.cgColor)
context.fill(rect)
img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let colorImage = img else { return nil }
return colorImage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment