Skip to content

Instantly share code, notes, and snippets.

@isoiphone
Created March 28, 2016 16:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isoiphone/031da3656d69c0d85805 to your computer and use it in GitHub Desktop.
Save isoiphone/031da3656d69c0d85805 to your computer and use it in GitHub Desktop.
Create UIImage of solid color
extension UIImage {
class func imageWithColor(color: UIColor, size: CGSize=CGSize(width: 1, height: 1)) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(CGRect(origin: CGPoint.zero, size: size))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
@aplekhanov
Copy link

UIGraphicsGetImageFromCurrentImageContext return optional value, so it should have optional when return:

func imageWithColor(color: UIColor, size: CGSize=CGSize(width: 1, height: 1)) -> UIImage?

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