在Swift中重置CoreImage过滤器

时间:2016-05-29 13:26:27

标签: swift core-image

我正在开发Swift中的简单coreImage过滤器项目。我正在尝试实现将过滤后的图像重置为原始的功能。我正在使用以下代码进行SepiaTone过滤器,我正在尝试使用CIColorControls过滤器重置过滤器。但是我遇到了致命的崩溃。我想知道是否还有其他方法可以重置图像。

        @IBOutlet weak var originalImage: UIImageView!

        @IBAction func SepiaToneFilter(sender: AnyObject) {
        let mySepiaFilter = CIFilter(name: "CISepiaTone")
        mySepiaFilter!.setValue(CIImage(image: originalImage.image!), forKey: kCIInputImageKey)
        let myOutputImage : CIImage = mySepiaFilter!.outputImage!            
        originalImage.image = UIImage(CIImage: myOutputImage)
        }

        @IBAction func ResetFilter(sender: AnyObject) {

        let currentFilter = CIFilter(name: "CIColorControls") 
        let beginImage = CIImage(image: originalImage.image!)
        currentFilter!.setValue(beginImage, forKey: kCIInputImageKey
        let output = currentFilter!.outputImage 

        let cgimg = context.createCGImage(output!, fromRect: output!.extent)  **//CreateWrappedSurface() failed for a dataprovider-backed CGImageRef.fatal error: unexpectedly found nil while unwrapping an Optional value
     (lldb)** 

        let processedImage = UIImage(CGImage: cgimg)
        originalImage.image = processedImage
        } 

//选择图像处理

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    originalImage.image = image
    self.dismissViewControllerAnimated(true, completion: nil);
}

我不确定如何在 ResetFilter (UIButton)中运行上述功能。

先谢谢

1 个答案:

答案 0 :(得分:1)

为什么不直接对原始图像进行引用?例如:

function Random () {
  var randomNumber = Math.floor(Math.random() * 2)+ 1;
  return randomNumber;
}
alert(Random());