快速改变图像的色调 [英] changing the tint of the image in swift

查看:121
本文介绍了快速改变图像的色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是swift的新手,并试图实现这一目标,
此图片为

I am new to swift and trying to achieve this essentially, This image to

此图片 - >

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

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

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

If this is a noob question, apologies. I was able to do this easily in javascript but not in swift.

推荐答案

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

hi you can use it the following way. First the UIImage Extension that you used needs some updates. you can copy the code below for 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

Then in your viewDidLoad where you are using the image for example i used the image from 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

其他扩展我发现

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

这篇关于快速改变图像的色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆