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

后端 未结 8 841
-上瘾入骨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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 20:26

    macOS (NSImage) Swift 3 version of @FBente's conversion of @Shmidt's answer:

    extension NSImage
    {
        func imageBlackAndWhite() -> NSImage?
        {
            if let cgImage = self.cgImage(forProposedRect: nil, context: nil, hints: nil)
            {
                let beginImage = CIImage.init(cgImage: cgImage)
                let paramsColor: [String : AnyObject] = [kCIInputBrightnessKey: NSNumber(value: 0.0),
                                                         kCIInputContrastKey:   NSNumber(value: 1.1),
                                                         kCIInputSaturationKey: NSNumber(value: 0.0)]
                let blackAndWhite = beginImage.applyingFilter("CIColorControls", withInputParameters: paramsColor)
    
                let paramsExposure: [String : AnyObject] = [kCIInputEVKey: NSNumber(value: 0.7)]
                let output = blackAndWhite.applyingFilter("CIExposureAdjust", withInputParameters: paramsExposure)
    
                if let processedCGImage = CIContext().createCGImage(output, from: output.extent) {
                    return NSImage(cgImage: processedCGImage, size: self.size)
                }
            }
            return nil
        }
    }
    

提交回复
热议问题