Skip to content

Instantly share code, notes, and snippets.

@jaemyeong
Last active March 18, 2018 14:17
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 jaemyeong/c0e57c5b2396cd69abdc20b97bbf4a41 to your computer and use it in GitHub Desktop.
Save jaemyeong/c0e57c5b2396cd69abdc20b97bbf4a41 to your computer and use it in GitHub Desktop.
import UIKit
extension UIImage {
public convenience init(color: UIColor, size: CGSize) {
UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
defer {
UIGraphicsEndImageContext()
}
let context = UIGraphicsGetCurrentContext()
assert(context != nil)
context!.setFillColor(color.cgColor)
context!.fill(CGRect(origin: .zero, size: size))
let image = context!.makeImage()
assert(image != nil)
self.init(cgImage: image!, scale: UIScreen.main.scale, orientation: UIImageOrientation.up)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment