Skip to content

Instantly share code, notes, and snippets.

@hisoka0917
Last active May 8, 2018 09:31
Show Gist options
  • Save hisoka0917/87c847d0f1d62967f3a4276447041feb to your computer and use it in GitHub Desktop.
Save hisoka0917/87c847d0f1d62967f3a4276447041feb to your computer and use it in GitHub Desktop.
Get a UIImage with solid color
extension UIImage {
public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height:1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 1.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else {
return nil
}
self.init(cgImage: cgImage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment