Изменение цвета UIImage ⇐ IOS
-
Anonymous
Изменение цвета UIImage
It is necessary to change the color of the image to 00ff00. But it turns out to 4fff00.
I tried it in two ways. The result is the same.
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!) } or
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) } } Calling the class as follows:
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!) } I display it on the UIImageView. Where am I wrong?
Источник: https://stackoverflow.com/questions/781 ... or-uiimage
It is necessary to change the color of the image to 00ff00. But it turns out to 4fff00.
I tried it in two ways. The result is the same.
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!) } or
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) } } Calling the class as follows:
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!) } I display it on the UIImageView. Where am I wrong?
Источник: https://stackoverflow.com/questions/781 ... or-uiimage
Мобильная версия