更改UIImage的颜色

问题描述 投票:0回答:1

需要将图像的颜色更改为00ff00。 但结果是 4fff00.

我尝试了两种方法。结果是一样的。

internal static func imageColorMatrix1(image: UIImage) -> UIImage? {
    let colorMatrix = CIFilter(name: "CIColorMatrix")
    colorMatrix?.setDefaults()
    colorMatrix?.setValue(CIImage(cgImage: image.cgImage!), forKey: kCIInputImageKey)
    colorMatrix?.setValue(CIVector(x: 0.0, y: 0.0, z: 0.0, w: 0.0), forKey: "RVector")
    colorMatrix?.setValue(CIVector(x: 0.0, y: 0.0, z: 0.0, w: 0.0), forKey: "GVector")
    colorMatrix?.setValue(CIVector(x: 0.0, y: 0.0, z: 0.0, w: 0.0), forKey: "BVector")
    colorMatrix?.setValue(CIVector(x: 0.0, y: 0.0, z: 0.0, w: 1.0), forKey: "AVector")
    colorMatrix?.setValue(CIVector(x: 0.0, y: 1.0, z: 0.0, w: 0.0), forKey: "biasVector")
    let outIm = colorMatrix?.outputImage
    let cgIm = CIContext().createCGImage(outIm!, from: outIm!.extent)
    return UIImage(cgImage: cgIm!)
}

class ColorFilter: CIFilter {
    var inputImage: CIImage?
    var inputColor: UIColor?
    
    let kernel: CIColorKernel = {
       let kernelString = "kernel vec4 colorize(__sample pixel, vec4 color)\n"
        + "{\n"
        //+ "pixel.rgb = color.rgb;\n"
        + "pixel.r = 0.0;\n"
        + "pixel.g = 1.0;\n"
        + "pixel.b = 0.0;\n"
        + "return pixel;\n"
        + "}\n"
       
        return CIColorKernel(source: kernelString)!
    }()
    
    override var outputImage: CIImage? {
        guard let inputImage = inputImage else {
            return nil
        }
        guard let inputColor = inputColor else {
            return nil
        }
        
        let inputs = [inputImage, CIColor(color: inputColor)] as [Any]
        return kernel.apply(extent: inputImage.extent, arguments: inputs)
    }
}

调用类如下:

internal static func imageColorMatrix1(image: UIImage) -> UIImage? {
    let colorFilter = ColorFilter()
    colorFilter.inputColor = UIColor.green
    colorFilter.inputImage = CIImage(cgImage: image.cgImage!)
    let outIm = colorFilter.outputImage
    let context = CIContext()
    let cgIm = context.createCGImage(outIm!, from: outIm!.extent)
    return UIImage(cgImage: cgIm!)
}

我将其显示在 UIImageView 上。 我哪里错了?

ios swift uiimageview uiimage uicolor
1个回答
0
投票
 internal static func imageColorize(image: UIImage, color: UIColor) -> UIImage? {
    let ciImage = CIImage(image: image)
    let filter = CIFilter(name: "CIColorMonochrome")
    filter?.setValue(ciImage, forKey: kCIInputImageKey)
    filter?.setValue(CIColor(color: color), forKey: kCIInputColorKey)
    filter?.setValue(1.0, forKey: kCIInputIntensityKey)
    
    guard let outputImage = filter?.outputImage else { return nil }
    let context = CIContext()
    guard let cgImage = context.createCGImage(outputImage, from: outputImage.extent) else { return nil }
    
    return UIImage(cgImage: cgImage)
}

为了传递十六进制颜色创建一个扩展

 extension UIColor {
convenience init(hex: String, alpha: CGFloat = 1.0) {
    var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
    hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")

    var rgb: UInt64 = 0

    Scanner(string: hexSanitized).scanHexInt64(&rgb)

    let red = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
    let green = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
    let blue = CGFloat(rgb & 0x0000FF) / 255.0

    self.init(red: red, green: green, blue: blue, alpha: alpha)
 }
}

像这样使用它:

let greenImage = BaseViewController.imageColorize(image: UIImage(resource: .coin), color: UIColor(hex: "#00ff00"))

用你的控制器名称更改 BaseViewController

© www.soinside.com 2019 - 2024. All rights reserved.