Skip to content

Instantly share code, notes, and snippets.

@viccc
Created November 9, 2015 21:51
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save viccc/01e28adcdee5cf6b9980 to your computer and use it in GitHub Desktop.
Save viccc/01e28adcdee5cf6b9980 to your computer and use it in GitHub Desktop.
Create an empty UIImage of a given size, optionally filled with a given color. Swift.
func imageWithPixelSize(size: CGSize, filledWithColor color: UIColor = UIColor.clearColor(), opaque: Bool = false) -> UIImage {
return imageWithSize(size, filledWithColor: color, scale: 1.0, opaque: opaque)
}
func imageWithSize(size: CGSize, filledWithColor color: UIColor = UIColor.clearColor(), scale: CGFloat = 0.0, opaque: Bool = false) -> UIImage {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
color.set()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
@mtavkhelidze
Copy link

UIImage.getColoredRectImageWith(color: UIColor.black.cgColor, andSize: CGSize(width: 50, height: 50)

@CapnSpellcheck
Copy link

If you are suggesting there's such a UIImage method provided by Apple, I don't think so...

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