Skip to content

Instantly share code, notes, and snippets.

@Andy0570
Last active November 18, 2020 03:00
Show Gist options
  • Save Andy0570/a247a00d223c356eeb8e02f281bdae09 to your computer and use it in GitHub Desktop.
Save Andy0570/a247a00d223c356eeb8e02f281bdae09 to your computer and use it in GitHub Desktop.
通过颜色生成图片
extension UIColor {
func image(_ size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
return UIGraphicsImageRenderer(size: size).image { (rendererContext) in
self.setFill()
rendererContext.fill(CGRect(origin: .zero, size: size))
}
}
}
@Andy0570
Copy link
Author

Usage:

 let image0 = UIColor.orange.image(CGSize(width: 128, height: 128))
 let image1 = UIColor.yellow.image()

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