-
Notifications
You must be signed in to change notification settings - Fork 1
/
UIImage+Color.swift
32 lines (24 loc) · 956 Bytes
/
UIImage+Color.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
extension UIImage {
class func imageFrom(color: UIColor, size: CGSize = CGSize(width: 1, height: 1) ) -> UIImage {
let rect = CGRect(origin: CGPoint(x: 0, y:0), size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
defer {
UIGraphicsEndImageContext()
}
if let context = UIGraphicsGetCurrentContext() {
context.setFillColor(color.cgColor)
context.fill(rect)
}
return UIGraphicsGetImageFromCurrentImageContext()!
}
class func imageFrom(layer: CALayer) -> UIImage {
UIGraphicsBeginImageContextWithOptions(layer.frame.size, layer.isOpaque, 0.0)
defer {
UIGraphicsEndImageContext()
}
if let context = UIGraphicsGetCurrentContext() {
layer.render(in: context)
}
return UIGraphicsGetImageFromCurrentImageContext()!
}
}