iOS 修改图片颜色

2,067 阅读1分钟

颜色转图片


class func imageWithColor( _ color:UIColor) -> UIImage {

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

UIGraphicsBeginImageContext(rect.size)

let context = UIGraphicsGetCurrentContext()

context?.setFillColor(color.cgColor)

context?.fill(rect)

let image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return image!

}

修改图片颜色


func yx_imageChangeColor( _ color:UIColor) -> UIImage? {

UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

let context = UIGraphicsGetCurrentContext()

context?.translateBy(x: 0, y: self.size.height)

context?.scaleBy(x: 1.0, y: -1.0)

context?.setBlendMode(CGBlendMode.normal)

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

context?.clip(to: rect, mask: self.cgImage!)

color.setFill()

context?.fill(rect)

let newImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return newImage

}