Sign up for our live Camera Kit office hour on July 17 @ 9:30am (PT) / 4:30pm (GMT)

Requesting assistance for to close the Camera Kit View when the button clicked?

Options
AliveNow Creative Tech
AliveNow Creative Tech Posts: 20 🔥
edited January 2023 in General #1

Hi All, I am integrating a Camera Kit into a new IOS project and I need some help with the following scenario:

My scenario is when I click the button from ViewController it has to load the Camera Kit view and when I click the back button from Camera Kit view it has to go to ViewController.(i.e Main Storyboard)

I am able to load the Camera Kit View when I click the button from ViewController. When I click the back button from Camera Kit View It was going back to ViewController but the AVSessionInput has not stopped and the Camera is still running in the background.

How can I close the AVSessionInput? Can anyone please help me with out this?

Thank you.

Answers

  • Bakari Mustafa
    Bakari Mustafa Posts: 178 🔥🔥🔥
    Options

    To stop the AVSessionInput when the user navigates back to your ViewController, you'll need to stop the session explicitly. You can do this by calling the stopRunning method on the AVCaptureSession object that you're using to capture video from the camera.

    Here's an example of how you might stop the session when the user navigates back to your ViewController:

    In your CameraKit view controller's viewWillDisappear: method, you can stop the session:

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        // Stop the AVCaptureSession
        cameraKit.stopSession()
    }
    
    

    In your CameraKit view controller class, define a method to stop the session.

    func stopSession() {
        self.session.stopRunning()
    }
    

    Alternatively, you can stop the session in the viewWillDissapear method of the view controller that you're navigating back to.

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        // Stop the AVCaptureSession
        cameraKit.stopSession()
    }
    

    This way, when the user navigates back to your ViewController, the camera session will be stopped automatically.

  • AliveNow Creative Tech
    Options

    Hi @Bakari Mustafa, Thank you so much for your response. I have tried the solution that you have shared but the Camera Kit view has not closed properly. The Camera View is still running in the background on my ViewController.

    public func stopCameraKitSession()
    {
    cameraKit.stop();
    AVSessionInput().stopRunning();
    }

    This is the method that I have created in my Camera View controller and called in viewWillAppear method.

    Can you please help me out with this?

    Thank you.

  • Bakari Mustafa
    Bakari Mustafa Posts: 178 🔥🔥🔥
    Options

    It sounds like the issue may be that you're not properly dismissing the camera view controller when you're done with it. One way to do this is to use the dismiss(animated:completion:) method on the view controller that presented the camera view controller.
    You can call this method in the same stopCameraKitSession method after calling cameraKit.stop() and AVSessionInput().stopRunning() .
    So your stopCameraKitSession method should look

    public func stopCameraKitSession() {
        cameraKit.stop()
        AVSessionInput().stopRunning()
        self.dismiss(animated: true, completion: nil)
    }
    

    It will dismiss the current view controller and you'll go back to the view controller that originally presented it.

    Let me know if it helps.

  • AliveNow Creative Tech
    Options

    Hi @Bakari Mustafa, Thank you so much for all your responses. I have tried all your suggestions but the Camera Kit view has not closed properly. Please check the below screenshot:

    The Camera View still running in the background even after changing the view from CameraView to Main View, please check the arrow indications on the above image.

    These are below steps that I have followed to close the Camera Kit view when I click the back button from the Camera Kit viewer UI.

    Added the below method to AppDelegate:

    public func resetApp() {
    UIApplication.shared.windows[0].rootViewController = UIStoryboard(
    name: "Main",
    bundle: nil
    ).instantiateInitialViewController()
    UIApplication.shared.windows[0].makeKeyAndVisible()
    }

    Called the resetApp() method from the Camera Kit View like below:

    @objc func backButtonAction(_ sender: UIButton) {

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.resetApp();
    }
    

    Added the viewWillDisappear method to the Camera Kit View like this:

    override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

        let customizedOBJ = CustomizedCameraController()
        customizedOBJ.stopCameraKitSession()
        self.dismiss(animated: true, completion: nil)
    }
    

    CustomizedCameraController is a class that is inheriting the CameraController class

    Below is the method that I added to stop camerakit and AVSessionInput:

    public func stopCameraKitSession()
    {
    cameraKit.stop()
    AVSessionInput(session: captureSession).stopRunning()
    }

    Can you please check all these methods and let me know what I am missing?

    Thank you.

  • Bakari Mustafa
    Bakari Mustafa Posts: 178 🔥🔥🔥
    Options

    It appears that you have implemented the right strategy to end the CameraKit session and reset the root view controller based on the steps you've provided, there might be a problem with the implementation.

    The dismiss method is being used before the CameraKit session has completely ended, which could be the root of the Camera View's background operation. You should make sure that you are stopping the CameraKit session on the same instance that was used to start it. You can achieve this by keeping a reference to the CustomizedCameraController instance in the CameraKit View and using that reference to stop the session.

    class CameraKitViewController: UIViewController {
        var customizedCameraController: CustomizedCameraController!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            customizedCameraController = CustomizedCameraController()
            // start the CameraKit session
        }
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            customizedCameraController.stopCameraKitSession() {
                self.dismiss(animated: true, completion: nil)
            }
        }
    }
    

    This should fix the issue of the Camera View still running in the background after changing the view.

    Make sure you are also stopping the AVSessionInput properly and also check that the AVSessionInput class is being initialized with the correct AVCaptureSession.

    Please let me know how you go, Thanks :)

  • AliveNow Creative Tech
    AliveNow Creative Tech Posts: 20 🔥
    edited January 2023 #7
    Options

    Hi @Bakari Mustafa, Thank you so much for your help. I have tried all of your suggestions but still,
    the Camera view running in the background issue was not fixed. Also, the Lens that we have opened is still running in the background along with the camera after changing the view.

    Please check the below screenshots :

    1. resetApp method is used to reset rootViewcontroller and to change the view from Camera View to View Controller.

    1. Created variable customizedCameraController and viewDidLoad and viewWillDisappear and backButtonAction are used from the above screenshot in a CustomizedCameraViewController class.

    1. Created a sessionInput variable to hold the AVSessionInput in CameraController class

    1. Initialized the sessionInput as an AVSessionInput(session: captureSession) in CameraController class

    1. Started the cametakit session with sessionInput instance in CameraController class

    1. Created stopCameraKitSession to stop Camerakit Session and AVSessionInput in CameraController class.

  • AliveNow Creative Tech
    Options

    Also, Can I get your email support so that I can explain the steps that I am following to fix the Camera View running in the background.

  • Bakari Mustafa
    Bakari Mustafa Posts: 178 🔥🔥🔥
    Options

    Based on the steps you've provided, it seems like the issue is that the CameraKit session is not being stopped completely before dismissing the CameraKit view. To properly solve this issue, you can try the following steps:

    • Keep a reference to the CustomizedCameraController instance in the CameraKit View and use that reference to stop the session. This will ensure that the session is being stopped on the same instance that was used to start it.
    • Make sure that the AVSessionInput class is being initialized with the correct AVCaptureSession. This will ensure that the AVSessionInput is being stopped correctly.
    • Use a completion handler in the stopCameraKitSession method. This will ensure that the completion block is called after the session is stopped and that the CameraKit view is dismissed only after the session has been stopped completely.
    • Try to remove the CameraView from the window instead of resetting the rootViewController to the initial view controller.
    • You can also try to remove the lens from the window using the removeFromSuperview() method
  • Bakari Mustafa
    Bakari Mustafa Posts: 178 🔥🔥🔥
    Options

    Also, If you would like to explain the steps you are following to fix the issue, you can reach out to the following email address, contact@bakarimustafa.com and I will do my best to assist you, Cheers :)

  • arashp
    arashp Posts: 52 🔥🔥
    Options

    When I click the back button from Camera Kit View It was going back to ViewController but the AVSessionInput has not stopped and the Camera is still running in the background.

    Thanks @AliveNow Creative Tech, @Bakari Mustafa. Let's say you are in the CameraViewController, or at least somewhere where you a reference to CameraController. To stop the AVSessionInput:

    if cameraController.cameraKit.activeInput.isRunning == true {
        self.cameraController.cameraKit.activeInput.stopRunning()
    } 
    

    And then to start it back up you would do this:

    if self.cameraController.cameraKit.activeInput.isRunning == false {
        self.cameraController.cameraKit.activeInput.startRunning()
    }
    
  • AliveNow Creative Tech
    Options

    Hi @arashp, I have tried to start and stop the AVSessionInput as you mentioned but I am getting the exception while calling the stopCameraKitSession method, Please check the below screenshot for exception logs:

    Below are the steps that I have followed:

    Created startCameraKitSession and stopCameraKitSession in CameraController class and called startCameraKitSession method when configuring Lenses.

    Called the stopCameraKitSession when clicking the back button from CustomizedCameraViewController class.

  • arashp
    arashp Posts: 52 🔥🔥
    Options

    Hi @AliveNow Creative Tech, thanks for the explanation and screenshots. I suspect you want to do this in CameraViewController, for example CameraViewController.cameraButtonHoldEnded(_:). What is the exception you are getting?

  • AliveNow Creative Tech
    Options

    Hi @arashp, I have integrated the Camera Kit SDK into a new IOS project. I was able to integrate the SDK successfully and tried the different Lenses that we built.

    Now What I want is when I click the button the Camera Kit View has to exit and it has to go to the App's Main View (i.e ViewController).

    I have tried the below way to integrate the Camera Kit SDK into my new IOS project and added the back button to Camera View.

    Created a Swift file and added all the variables and methods to this Swift file. I have done this by copying all the variables and methods from the Camera Kit SDK IOS sample GitHub repository's AppDelegate.

    Below are the steps that I did to exit Camera Kit View when the back button is clicked.

    Cleared the applied Lens
    Changed the rootViewController from Camera Kit View to App Main View (i.e ViewController)
    Stopped the Camera Kit session and AVInputSession like this:

    public func stopCameraKitSession()
    {
    cameraKit.stop()
    avSessionInput.stopRunning()
    }

    Please check all these and let us know if is this the way to exit Camera Kit View or if Is there any other way to exit the Camera Kit View when the button is clicked so it has to go App's Main View.

    Thank you.