2019独角兽企业重金招聘Python工程师标准>>>
这是一个UIImage集合类,可以很方便的对图片的染料(着色),增加亮度(闪电)和降低亮度(黑)和其他扩展的功能模块。
在swift下实现图片染色
import UIKitextension UIImage {/// Just change a colored color image, is to change the image TintColo////// - Parameter tintColor: Need to change the color of the color/// - Returns: Returns an imagefunc imageWithTintColor (tintColor: UIColor? = nil, alpha: CGFloat) -> UIImage? {let rect = CGRect(x: 0.0, y: 0.0, width: self.size.width, height: self.size.height)UIGraphicsBeginImageContextWithOptions(rect.size,false,self.scale)draw(in: rect)let ctx = UIGraphicsGetCurrentContext()ctx!.setFillColor((tintColor?.cgColor)!)ctx!.setAlpha(alpha);ctx?.setBlendMode(.sourceAtop)ctx!.fill(rect);let result = UIGraphicsGetImageFromCurrentImageContext()UIGraphicsEndImageContext()return result}
}
Usage To swift
override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.let gift01ImageView = UIImageView(image: UIImage(named: "ic-gift"))gift01ImageView.frame = CGRect(x: 50.0, y: 50.0, width: 48.0, height: 48.0)self.view!.addSubview(gift01ImageView)let gift02ImageView = UIImageView(image: UIImage(named: "ic-gift")?.imageWithTintColor(tintColor: UIColor.blue, alpha: 0.5))gift02ImageView.frame = CGRect(x: 125.0, y: 50.0, width: 48.0, height: 48.0)self.view!.addSubview(gift02ImageView)}
gitHub Demo下载地址