Skip to content

Instantly share code, notes, and snippets.

@Andy0570
Last active November 18, 2020 02:59
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 Andy0570/9102beeb1a499e027f0cb614e4da635b to your computer and use it in GitHub Desktop.
Save Andy0570/9102beeb1a499e027f0cb614e4da635b to your computer and use it in GitHub Desktop.
通过颜色生成图片
extension UIImage {
class func imageWithColor(color: UIColor) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
@Andy0570
Copy link
Author

Usage:

let image = UIImage.imageWithColor(color: UIColor.red)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment