网上找的第三方,color都是字符串去取,内部实现还需要去转一层,看着烦,就简单写了方法
import UIKit
extension UIColor {
/// hexColor
convenience init(hex: UInt32) {
let r: CGFloat = CGFloat((hex & 0xFF000000) >> 24) / 255.0
let g: CGFloat = CGFloat((hex & 0x00FF0000) >> 16) / 255.0
let b: CGFloat = CGFloat((hex & 0x0000FF00) >> 8) / 255.0
let a: CGFloat = CGFloat(hex & 0x000000FF) / 255.0
self.init(red: r, green: g, blue: b, alpha: a)
}
/// 创建一张纯色的图片的方法
func toImage(size: CGSize) -> UIImage {
UIGraphicsBeginImageContext(size);
let context = UIGraphicsGetCurrentContext();
context?.setFillColor(self.cgColor)
context?.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image!;
}
}