Skip to content

Instantly share code, notes, and snippets.

@ahmetgeymen
Last active September 19, 2017 17:36
Show Gist options
  • Save ahmetgeymen/b4bc5fe1fed8711da8e0a2d4eb405185 to your computer and use it in GitHub Desktop.
Save ahmetgeymen/b4bc5fe1fed8711da8e0a2d4eb405185 to your computer and use it in GitHub Desktop.
Extension to UIImage which returns an image filled with given solid color and given size.
import UIKit
extension UIImage {
static func imageWithColor(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage? {
UIGraphicsBeginImageContext(size)
if let context = UIGraphicsGetCurrentContext() {
context.setFillColor(color.cgColor)
context.fill(CGRect(origin: .zero, size: size))
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment