title: iOS How to close camera after successfully scanning QR code Swift 4
body: <p>How I can close camera after successfully scanning QR code? My problem is when I scan code from QR, camera is not closed after scanning. How I can close camera?
  My code:</p>
  
  <pre><code>var captureSession: AVCaptureSession!
  var previewLayer: AVCaptureVideoPreviewLayer!
  var qrCode = String()
  
  func failed() {
          let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert)
          ac.addAction(UIAlertAction(title: "OK", style: .default))
          present(ac, animated: true)
          captureSession = nil
      }
  
      func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
          captureSession.stopRunning()
  
          if let metadataObject = metadataObjects.first {
              guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
              guard let stringValue = readableObject.stringValue else { return }
              AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
              found(code: stringValue)
          }
  
          dismiss(animated: true)
  
      }
  
      func found(code: String) {
          print(code)
          qrCode = code
      }
  
      override var prefersStatusBarHidden: Bool {
          return true
      }
  
      override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
          return .portrait
      }
  </code></pre>
  
  <p>Button which open camera:</p>
  
  <pre><code> @IBAction func scanQrButton(_ sender: Any) {
      view.backgroundColor = UIColor.black
      captureSession = AVCaptureSession()
  
      guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
      let videoInput: AVCaptureDeviceInput
  
      do {
          videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
      } catch {
          return
      }
  
      if (captureSession.canAddInput(videoInput)) {
          captureSession.addInput(videoInput)
      } else {
          failed()
          return
      }
  
      let metadataOutput = AVCaptureMetadataOutput()
  
      if (captureSession.canAddOutput(metadataOutput)) {
          captureSession.addOutput(metadataOutput)
  
          metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
          metadataOutput.metadataObjectTypes = [.qr]
      } else {
          failed()
          return
      }
  
      previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
      previewLayer.frame = view.layer.bounds
      previewLayer.videoGravity = .resizeAspectFill
      view.layer.addSublayer(previewLayer)
  
      captureSession.startRunning()
  }
  </code></pre>
  
  <p>As you can see, I have code which opens the camera and scan QR, but I want to close camera after the code is scanned without segue.</p>