如何使用 Core Image 将色调应用到 UIImage

Cal*_*leb 2 rgb core-image uiimage swift

我正在寻找在不改变分辨率和尺寸的情况下将色调应用于图像的解决方案。\xe2\x80\xa8我正在使用 RGB 值和CIColorControls. 我能够应用不同的颜色控制(即亮度、对比度和饱和度)。

\n\n

我的值\n对比度、亮度、饱和度为(110%., 110%, 130%)\n并且我已经应用了它的工作正常。到目前为止,这是我的代码:\xe2\x80\xa8\xe2\x80\xa8

\n\n
func applyCustomValues(image: UIImage, brightness: Double, contrast: Double, saturation: Double) -> UIImage{\n\n    let beginImage = CIImage(cgImage: image.cgImage!)\n    let parameters = [\n        "inputContrast": NSNumber(value: contrast),\n        "inputBrightness": NSNumber(value: brightness),\n        "inputSaturation": NSNumber(value: saturation),\n        ]\n    let outputImage = beginImage.applyingFilter("CIColorControls", parameters: parameters)\n\n    let context = CIContext(options: nil)\n\n    let outputCGImage = context.createCGImage(outputImage, from: outputImage.extent)\n\n    return UIImage(cgImage: outputCGImage)\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的 RGB 值是 R = 243、G = 106、B = 188。

\n\n

我想将所有这些值应用到图像上并期望输出符合要求

\n\n

应用色调颜色(RGB)这是我到目前为止的代码:\xe2\x80\xa8\xe2\x80\xa8

\n\n
func tint(image: UIImage, color: UIColor) -> UIImage\n{\n    let ciImage = CIImage(image: image)\n    let filter = CIFilter(name: "CIMultiplyCompositing")\n    filter?.setDefaults()\n\n    let colorFilter = CIFilter(name: "CIConstantColorGenerator")\n    let ciColor = CIColor(color: color)\n    colorFilter?.setValue(ciColor, forKey: kCIInputColorKey)\n    let colorImage = colorFilter?.outputImage\n\n    filter?.setValue(colorImage, forKey: kCIInputImageKey)\n    filter?.setValue(ciImage, forKey: kCIInputBackgroundImageKey)\n    let context = CIContext(options: nil)\n    let cgImage = context.createCGImage(filter!.outputImage!, from: (ciImage?.extent)!)\n    return UIImage(cgImage: cgImage!)\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

要应用 Tint 和 CIColorControls,

\n\n
   let tintImage = tint(image:orinalImage, color: UIColor.getTintColor(r: 255/255, g: 131/255, b: 0/255, alpha: 1))\n\n   let colorControlImage = applyCustomValues(image: tintImage, brightness: 0.11, contrast: 1.10, saturation: 1.3)\n
Run Code Online (Sandbox Code Playgroud)\n\n

它返回下面附加的输出图像,

\n\n

在此输入图像描述

\n\n

原图是,

\n\n

在此输入图像描述

\n\n

预期输出图像\n请忽略图片比例\n请忽略图片比例

\n\n

请纠正我,如果我错了,实现这一目标的正确方法是什么。

\n

Cal*_*leb 7

最后,我得到了我想要的答案,希望对某人有帮助。

func colorized(with color: UIColor) -> UIImage? {
    guard
        let ciimage = CIImage(image: self),
        let colorMatrix = CIFilter(name: "CIColorMatrix")
        else { return nil }
    var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
    color.getRed(&r, green: &g, blue: &b, alpha: &a)
    colorMatrix.setDefaults()
    colorMatrix.setValue(ciimage, forKey: "inputImage")
    colorMatrix.setValue(CIVector(x: r, y: 0, z: 0, w: 0), forKey: "inputRVector")
    colorMatrix.setValue(CIVector(x: 0, y: g, z: 0, w: 0), forKey: "inputGVector")
    colorMatrix.setValue(CIVector(x: 0, y: 0, z: b, w: 0), forKey: "inputBVector")
    colorMatrix.setValue(CIVector(x: 0, y: 0, z: 0, w: a), forKey: "inputAVector")
    if let ciimage = colorMatrix.outputImage {
        return UIImage(ciImage: ciimage)
    }
    return nil
}
Run Code Online (Sandbox Code Playgroud)