Last active
May 8, 2018 09:31
-
-
Save hisoka0917/87c847d0f1d62967f3a4276447041feb to your computer and use it in GitHub Desktop.
Get a UIImage with solid color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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