title: Capture image with GPUImage filter on Swift
body: <p>I'm using GPUImage Framework for Image filters.</p>
  
  <p>I have applied the ToonFilter() on camera for cartoon filter effect.</p>
  
  <p>My issue is while capture image on camera with ToonFilter effect with GPUImage.</p>
  
  <p>Below is the code to show the ToonFilter with GPUImage camera &amp; to capture a new image with the same effect.</p>
  
  <p>Please guide me how I may capture an image with the same filter?</p>
  
  <p><strong>Note :</strong> I am successfully able to open camera with ToonFilter effect - the issue is only with capture a new image with a filter effect.</p>
  
  <p><strong>Code :</strong></p>
  
  <pre><code>import UIKit
  import GPUImage
  import AVFoundation
  
  class SelfieFilterVC: UIViewController, UISplitViewControllerDelegate  {
  
      @IBOutlet var filterSlider: UISlider?
      @IBOutlet var filterView: RenderView?
  
      let videoCamera:Camera?
      var blendImage:PictureInput?
  
      override func viewDidLoad() {
  
          super.viewDidLoad()
  
          self.filterOperation = FilterOperation(
              filter:{ToonFilter()},
              listName:"Toon",
              titleName:"Toon",
              sliderConfiguration:.disabled,
              sliderUpdateCallback: nil,
              filterOperationType:.singleInput
          )
  
          self.configureView()
      }
  
      required init(coder aDecoder: NSCoder)
      {
          do {
              videoCamera = try Camera(sessionPreset:AVCaptureSessionPreset640x480, location:.backFacing)
              videoCamera!.runBenchmark = true
  
          } catch {
              videoCamera = nil
              print("Couldn't initialize camera with error: \(error)")
          }
  
          super.init(coder: aDecoder)!
      }
  
      var filterOperation: FilterOperationInterface?
  
  
      @IBAction func btnCapture(_ sender: Any) {
  
          videoCamera?.startCapture()
  
          let pictureOutput = PictureOutput()
          pictureOutput.encodedImageFormat = .jpeg
          pictureOutput.encodedImageAvailableCallback = {imageData in
  
              if imageData != nil {
                  let captureDetailVC = self.storyboard?.instantiateViewController(withIdentifier: "CaptureDetailVC") as! CaptureDetailVC
                  captureDetailVC.aCaptureSelectedData = imageData
                  self.show(captureDetailVC, sender: true)
              }
          }
      }
  
      @IBAction func btnBackAction(_ sender: Any) {
          self.dismiss(animated: true, completion: nil)
      }
      func configureView() {
  
          guard let videoCamera = videoCamera else {
  
              let errorAlertController = UIAlertController(title: NSLocalizedString("Error", comment: "Error"), message: "Couldn't initialize camera", preferredStyle: .alert)
              errorAlertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK"), style: .default, handler: nil))
              self.present(errorAlertController, animated: true, completion: nil)
              return
          }
  
          if let currentFilterConfiguration = self.filterOperation {
              self.title = currentFilterConfiguration.titleName
  
              // Configure the filter chain, ending with the view
              if let view = self.filterView {
                  switch currentFilterConfiguration.filterOperationType {
                  case .singleInput:
                      videoCamera.addTarget(currentFilterConfiguration.filter)
                      currentFilterConfiguration.filter.addTarget(view)
                  case .blend:
                      videoCamera.addTarget(currentFilterConfiguration.filter)
                      self.blendImage = PictureInput(imageName:blendImageName)
                      self.blendImage?.addTarget(currentFilterConfiguration.filter)
                      self.blendImage?.processImage()
                      currentFilterConfiguration.filter.addTarget(view)
                  case let .custom(filterSetupFunction:setupFunction):
                      currentFilterConfiguration.configureCustomFilter(setupFunction(videoCamera, currentFilterConfiguration.filter, view))
                  }
  
                  videoCamera.startCapture()
              }
  
              // Hide or display the slider, based on whether the filter needs it
              if let slider = self.filterSlider {
                  switch currentFilterConfiguration.sliderConfiguration {
                  case .disabled:
                      slider.isHidden = true
                  //                case let .Enabled(minimumValue, initialValue, maximumValue, filterSliderCallback):
                  case let .enabled(minimumValue, maximumValue, initialValue):
                      slider.minimumValue = minimumValue
                      slider.maximumValue = maximumValue
                      slider.value = initialValue
                      slider.isHidden = false
                      self.updateSliderValue()
                  }
              }
  
          }
      }
  
      @IBAction func updateSliderValue() {
          if let currentFilterConfiguration = self.filterOperation {
              switch (currentFilterConfiguration.sliderConfiguration) {
              case .enabled(_, _, _): currentFilterConfiguration.updateBasedOnSliderValue(Float(self.filterSlider!.value))
              case .disabled: break
              }
          }
      }
  
  
  
      override func viewWillDisappear(_ animated: Bool) {
          if let videoCamera = videoCamera {
              videoCamera.stopCapture()
              videoCamera.removeAllTargets()
              blendImage?.removeAllTargets()
          }
  
          super.viewWillDisappear(animated)
      }
  }
  </code></pre>