快速改变图像的色调

我是新手迅速并试图实现这一点,这个图像来 在此处输入图像描述

这张图片 – >

在此处输入图像描述

我从这里使用此代码来更改图像上的色调,但没有得到所需的输出

func tint(image: UIImage, color: UIColor) -> UIImage { let ciImage = CIImage(image: image) let filter = CIFilter(name: "CIMultiplyCompositing") let colorFilter = CIFilter(name: "CIConstantColorGenerator") let ciColor = CIColor(color: color) colorFilter.setValue(ciColor, forKey: kCIInputColorKey) let colorImage = colorFilter.outputImage filter.setValue(colorImage, forKey: kCIInputImageKey) filter.setValue(ciImage, forKey: kCIInputBackgroundImageKey) return UIImage(CIImage: filter.outputImage)! } 

如果这是一个noob问题,请道歉。 我能够在javascript中轻松完成此操作,但不能在swift中轻松完成。

嗨,您可以通过以下方式使用它。 首先,您使用的UIImage扩展需要一些更新。 你可以复制下面的Swift 3代码

 extension UIImage{ func tint(color: UIColor, blendMode: CGBlendMode) -> UIImage { let drawRect = CGRect(x: 0,y: 0,width: size.width,height: size.height) UIGraphicsBeginImageContextWithOptions(size, false, scale) color.setFill() UIRectFill(drawRect) draw(in: drawRect, blendMode: blendMode, alpha: 1.0) let tintedImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return tintedImage! } } 

然后在你正在使用图像的viewDidLoad中,我使用了IBOutlet imageWood中的图像

  override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.imageWood.image = self.imageWood.image?.tint(color: UIColor.green, blendMode: .saturation) } 

您将必须使用适当的颜色和图像

我找到的另一个扩展

 extension UIImage { // colorize image with given tint color // this is similar to Photoshop's "Color" layer blend mode // this is perfect for non-greyscale source images, and images that have both highlights and shadows that should be preserved // white will stay white and black will stay black as the lightness of the image is preserved func tint(_ tintColor: UIColor) -> UIImage { return modifiedImage { context, rect in // draw black background - workaround to preserve color of partially transparent pixels context.setBlendMode(.normal) UIColor.black.setFill() context.fill(rect) // draw original image context.setBlendMode(.normal) context.draw(self.cgImage!, in: rect) // tint image (loosing alpha) - the luminosity of the original image is preserved context.setBlendMode(.color) tintColor.setFill() context.fill(rect) // mask by alpha values of original image context.setBlendMode(.destinationIn) context.draw(self.cgImage!, in: rect) } } // fills the alpha channel of the source image with the given color // any color information except to the alpha channel will be ignored func fillAlpha(_ fillColor: UIColor) -> UIImage { return modifiedImage { context, rect in // draw tint color context.setBlendMode(.normal) fillColor.setFill() context.fill(rect) // mask by alpha values of original image context.setBlendMode(.destinationIn) context.draw(self.cgImage!, in: rect) } } fileprivate func modifiedImage(_ draw: (CGContext, CGRect) -> ()) -> UIImage { // using scale correctly preserves retina images UIGraphicsBeginImageContextWithOptions(size, false, scale) let context: CGContext! = UIGraphicsGetCurrentContext() assert(context != nil) // correctly rotate image context.translateBy(x: 0, y: size.height); context.scaleBy(x: 1.0, y: -1.0); let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height) draw(context, rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } 

}

像这样使用它

 self.imageWood.image = self.imageWood.image?.tint(UIColor.purple.withAlphaComponent(1)) 

试试下面的代码,应该适合你。

  if let myImage = UIImage(named: "imageName")?.withRenderingMode(.alwaysTemplate) { myImageView.image = myImage myImageView.tintColor = UIColor.white } 

您的图像很简单,只有一种颜色。 我建议将图像透明,除非线条在哪里,然后将其分层放在白色背景上以获得结果。

看起来你得到了使用绘图模式后的结果。 还有许多Core图像filter可以让您对图像应用着色效果,以及替换特定颜色。 对于更复杂的图像,这些将是一个不错的选择。