What is the best Core Image filter to produce black and white effects?

后端 未结 8 836
-上瘾入骨i
-上瘾入骨i 2020-11-29 20:16

I am using Core Image and would like to produce a black and white effect on the chosen image.

Ideally I would like to have access to the same sort of options that a

8条回答
  •  悲哀的现实
    2020-11-29 20:21

    Here is the most liked answer from @Shmidt written as an UIImage extension with a performance update in Swift:

    import CoreImage
    
    extension UIImage
    {
        func imageBlackAndWhite() -> UIImage?
        {
            if let beginImage = CoreImage.CIImage(image: self)
            {
                let paramsColor: [String : AnyObject] = [kCIInputBrightnessKey: NSNumber(double: 0.0),
                                                         kCIInputContrastKey:   NSNumber(double: 1.1),
                                                         kCIInputSaturationKey: NSNumber(double: 0.0)]
                let blackAndWhite = beginImage.imageByApplyingFilter("CIColorControls", withInputParameters: paramsColor)
    
                let paramsExposure: [String : AnyObject] = [kCIInputEVKey: NSNumber(double: 0.7)]
                let output = blackAndWhite.imageByApplyingFilter("CIExposureAdjust", withInputParameters: paramsExposure)
    
                let processedCGImage = CIContext().createCGImage(output, fromRect: output.extent)
                return UIImage(CGImage: processedCGImage, scale: self.scale, orientation: self.imageOrientation)
            }
            return nil
        }
    }
    

提交回复
热议问题