How to create monochromatic UIImage on iOS I have a UIImage coming from server that I need to present in the UI as a monochromatic image with a given single color that can be an arbitrary as well. What's the best way to achieve it?

In my current method I am using following method that returns a monochromatic image for a given image and a color:    
fileprivate func monochromaticImage(from image: UIImage, in color: UIColor) -> UIImage {
    guard let img = CIImage(image: image) else {
return image
    }
    let color = CIColor(color: color)
    guard let outputImage = CIFilter(name: "CIColorMonochrome",
withInputParameters: ["inputImage" : img,
"inputColor" : color])?.outputImage  else {
return image
    }
    let context = CIContext()
    if let cgImage = context.createCGImage(outputImage, from: outputImage.extent) {
let newImage = UIImage(cgImage: cgImage, scale: image.scale, orientation: image.imageOrientation)
return newImage
    }
    return image
}