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

时间:2017-10-19 11:29:11

标签: firebase swift3 uiimage xcode8 firebase-storage

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

Caused by: java.lang.UnsupportedOperationException: Unsupported Uri content://com.android.providers.downloads.documents/tree/downloads

这就是我检索图像的方式

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"))

1 个答案:

答案 0 :(得分:0)

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
       }
    })
}

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