Created
April 3, 2018 17:54
-
-
Save mingsai/7de00af9d96bd9664066fcf89eb4a7ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIImage { | |
class func colorAsImage(from color:UIColor, for size:CGSize) -> UIImage? { | |
var img:UIImage? | |
let rect = CGRect(x:0.0, y:0.0, width: size.width, height: size.height) | |
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0) | |
guard let context = UIGraphicsGetCurrentContext() else { return nil } | |
context.setFillColor(color.cgColor) | |
context.fill(rect) | |
img = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
guard let colorImage = img else { return nil } | |
return colorImage | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment