Skip to content

Instantly share code, notes, and snippets.

@eleev
Created March 24, 2017 11:18
Show Gist options
  • Save eleev/ba27ed77bbe368c325f899814bfd83cc to your computer and use it in GitHub Desktop.
Save eleev/ba27ed77bbe368c325f899814bfd83cc to your computer and use it in GitHub Desktop.
Extension for creating UIImage from a UIColor. Swift 3
extension UIImage {
convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.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