Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Created April 16, 2019 02:34
Show Gist options
  • Save KentarouKanno/9e77702c7cb28aa737609736db7ad7b5 to your computer and use it in GitHub Desktop.
Save KentarouKanno/9e77702c7cb28aa737609736db7ad7b5 to your computer and use it in GitHub Desktop.
  • UIColorからUIImageを生成
extension UIImage {
    
    static func image(filled color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
        defer {
            UIGraphicsEndImageContext()
        }
        
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContext(rect.size)
        
        let contextRef = UIGraphicsGetCurrentContext()
        contextRef!.setFillColor(color.cgColor)
        contextRef!.fill(rect)
        
        return UIGraphicsGetImageFromCurrentImageContext()!
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment