Convert UIImage to grayscale keeping image quality

前端 未结 7 1175
难免孤独
难免孤独 2020-12-08 19:42

I have this extension (found in obj-c and I converted it to Swift3) to get the same UIImage but grayscaled:

public fun         


        
7条回答
  •  囚心锁ツ
    2020-12-08 20:23

    Joe's answer as an UIImage exension for Swift 4 working correctly for different scales:

    extension UIImage {
        var noir: UIImage {
            let context = CIContext(options: nil)
            let currentFilter = CIFilter(name: "CIPhotoEffectNoir")!
            currentFilter.setValue(CIImage(image: self), forKey: kCIInputImageKey)
            let output = currentFilter.outputImage!
            let cgImage = context.createCGImage(output, from: output.extent)!
            let processedImage = UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
    
            return processedImage
        }
    }
    

提交回复
热议问题