iOS swift-color转image

//传递颜色,size返回image

func imageFromColor(color: UIColor, viewSize: CGSize) -> UIImage{

let rect: CGRect = CGRect(x: 0, y: 0, width: viewSize.width, height: viewSize.height)

UIGraphicsBeginImageContext(rect.size)

let context: CGContext = UIGraphicsGetCurrentContext()!

context.setFillColor(color.cgColor)

context.fill(rect)

let image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsGetCurrentContext()

return image!

}

你可能感兴趣的:(iOS swift-color转image)