将 UImage 的颜色从 firebase 更改为黑白

Change colour of UImage from firebase to black and white

是否可以使用 url 从 firebase 加载图像,然后将其颜色更改为黑白? 如果我使用资产中的图像,它可以工作,但我通过 url 从 firebase 加载我的图像,当我尝试应用下面的代码时,backgroundProfileImage.image 等于 nil 或未检测到图像。如果有帮助,我还使用 SDWebImage 加载我的图像。我怎样才能使这项工作?我提前感谢帮助。谢谢

func ChangeImageColor(){
      //Auto Adjustment to Input Image
        var inputImage = CIImage(image: self.backgroundProfileImage.image!) //backgroundProfileImage is not 
        let options:[String : AnyObject] = [CIDetectorImageOrientation:1 as AnyObject]
        let filters = inputImage!.autoAdjustmentFilters(options: options)

        for filter: CIFilter in filters {
            filter.setValue(inputImage, forKey: kCIInputImageKey)
            inputImage =  filter.outputImage
        }
        let cgImage = self.context.createCGImage(inputImage!, from: inputImage!.extent)
        self.backgroundProfileImage.image =  UIImage(cgImage: cgImage!)

        //Apply noir Filter
        let currentFilter = CIFilter(name: "CIPhotoEffectTonal")
        currentFilter!.setValue(CIImage(image: UIImage(cgImage: cgImage!)), forKey: kCIInputImageKey)

        let output = currentFilter!.outputImage
       // let cgimg = self.context.createCGImage(output!, from: output!.extent)
        let processedImage = UIImage(cgImage: cgimg)
        self.backgroundProfileImage.image = processedImage
           }
        })
    }

这就是我检索图像的方式

self.backgroundProfileImage.sd_setImage(with: URL(string: backgroundProfileImageURL), placeholderImage: UIImage(named: "empty-profile-3.png"))

好的,这个方法对我有用

 func ChangeImageColor(){

   let currentFilter = CIFilter(name: "CIPhotoEffectNoir")
        currentFilter!.setValue(CIImage(image: self.backgroundProfileImage.image!), forKey: kCIInputImageKey)
        let output = currentFilter!.outputImage
        let cgimg = context.createCGImage(output!,from: output!.extent)
        let processedImage = UIImage(cgImage: cgimg!)
        self.backgroundProfileImage.image = processedImage
  }
imgView.sd_setImage(with: URL(string:url), completed: { (image, error, type, url) in
     //Do any thing with image here.
     self.ChangeImageColor(image)
 })


func ChangeImageColor(_ image: UIImage){
  //Auto Adjustment to Input Image
    var inputImage = CIImage(image: image)  
    let options:[String : AnyObject] = [CIDetectorImageOrientation:1 as AnyObject]
    let filters = inputImage!.autoAdjustmentFilters(options: options)

    for filter: CIFilter in filters {
        filter.setValue(inputImage, forKey: kCIInputImageKey)
        inputImage =  filter.outputImage
    }
    let cgImage = self.context.createCGImage(inputImage!, from: inputImage!.extent)
    self.backgroundProfileImage.image =  UIImage(cgImage: cgImage!)

    //Apply noir Filter
    let currentFilter = CIFilter(name: "CIPhotoEffectTonal")
    currentFilter!.setValue(CIImage(image: UIImage(cgImage: cgImage!)), forKey: kCIInputImageKey)

    let output = currentFilter!.outputImage
   // let cgimg = self.context.createCGImage(output!, from: output!.extent)
    let processedImage = UIImage(cgImage: cgimg)
    self.backgroundProfileImage.image = processedImage
       }
    })
}

当您使用库检索图像时调用该方法并将其作为参数传递。