快速改变图像的色调

时间:2017-07-26 12:54:03

标签: ios swift xcode uiimage

我是新手,并且试图实现这一目标, 这个图像来 enter image description here

此图片 - >

enter image description here

我正在使用此代码from here更改图片上的色调,但未获得所需的输出

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中轻松完成。

3 个答案:

答案 0 :(得分:3)

您可以通过以下方式使用它。首先,您使用的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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

您的图片很简单,只有一种颜色。我建议你的图像除了线条之外都是透明的,然后将它分层放在白色背景上以获得结果。

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