简体   繁体   English

快速改变图像的色彩

[英]changing the tint of the image in swift

I am new to swift and trying to achieve this essentially, This image to 我是新手,并试图从根本上实现这一目标, 在此处输入图片说明

This image -> 此图片->

在此处输入图片说明

I am using this code from here to change the tint on image but do not get the desired output 从这里使用此代码来更改图像的色调,但未获得所需的输出

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)!
}

If this is a noob question, apologies. 如果这是一个菜鸟问题,则表示歉意。 I was able to do this easily in javascript but not in swift. 我能够用javascript轻松做到这一点,但不能迅速做到这一点。

hi you can use it the following way. 您好,您可以按以下方式使用它。 First the UIImage Extension that you used needs some updates. 首先,您使用的UIImage扩展需要一些更新。 you can copy the code below for Swift 3 您可以复制下面的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!
}
}

Then in your viewDidLoad where you are using the image for example i used the image from IBOutlet imageWood 然后在您使用图像的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)
}

You will have to use the appropriate color and the images 您将必须使用适当的颜色和图像

The other Extension i found 我发现的另一个扩展

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!
}

} }

Using it like this 这样使用

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

Try the below code, should work for you. 试试下面的代码,应该适合您。

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

Your image is simple and only has one color. 您的图像很简单,只有一种颜色。 I would suggest making your image transparent except where the lines are, and then layer it on top of a white background to get your results. 我建议使您的图像透明(除了线条在哪里),然后将其放在白色背景上以得到您的结果。

It looks like you got the results you're after using drawing modes. 使用绘图模式后,看起来好像获得了结果。 There are also a number of Core image filters that let you apply tinting effects to images, as well as replacing specific colors. 还有许多Core图像过滤器,可让您对图像应用着色效果以及替换特定的颜色。 Those would be a good choice for more complex images. 对于更复杂的图像,这些将是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM