user10219864
user10219864

Reputation:

When I go back to menu, the App crashes (Thread 1: signal SIGABRT)

I am almost finished with my App "AR Note". There is only one bug I need to fix.

I have a main menu with 7 buttons linking to view controllers. To go back to main menu I use

@IBAction func unwindFromSources(segue: UIStoryboardSegue){
    }

In my old version this works perfectly fine. All I changed is, that the background of my main menu is a blurred camera view, which works pretty good. Now, with this blur-camera-view-background, the App always crashes with Thread 1: signal SIGABRT! This seems to me awkward because it says no errors, and without the blurred camera view background the back-buttons are working fine.

Here is my main menu code:

import UIKit
import AVFoundation

class ViewControllerBlurrHome: UIViewController {

    @IBAction func unwindFromSources(segue: UIStoryboardSegue){
    }

    let session: AVCaptureSession = AVCaptureSession()

    override func viewDidLoad() {
        super.viewDidLoad()
    }



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

        session.sessionPreset = AVCaptureSession.Preset.high

        if let device = AVCaptureDevice.default(for: AVMediaType.video) {
            do {
                try session.addInput(AVCaptureDeviceInput(device: device))

            } catch {
                print(error.localizedDescription)
            }

            let previewLayer = AVCaptureVideoPreviewLayer(session: session)

            self.view.layer.insertSublayer(previewLayer,at:0)
            previewLayer.frame = self.view.layer.bounds

        }



        session.startRunning()

        let blur = UIBlurEffect(style: .regular)
        let blurView = UIVisualEffectView(effect: blur)
        blurView.frame = self.view.bounds
        blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.view.insertSubview(blurView,at:1)

    }


// ShareButton
    @IBAction func sharePressed(_ sender: Any) {
        let activityVC = UIActivityViewController(activityItems: ["enter App Link here"], applicationActivities: nil)
        activityVC.popoverPresentationController?.sourceView = self.view
        self.present(activityVC, animated: true, completion: nil)
    }


}

This is how my main menu looks like:

image1

This is how my main.storyboard looks like:

image2

This is how I use the back-buttons:

image3

This is an alternative viewcontroller.swift, without the blur stuff, which works perfectly fine:

import UIKit

class ViewControllerBasis: UIViewController {


    @IBAction func unwindFromSources(segue: UIStoryboardSegue){
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    @IBAction func sharePressed(_ sender: Any) {
        let activityVC = UIActivityViewController(activityItems: ["enter App Link here"], applicationActivities: nil)
        activityVC.popoverPresentationController?.sourceView = self.view
        self.present(activityVC, animated: true, completion: nil)
    }

DO you mean this?

image4

There is this:

image5

and this:

image6

Upvotes: 1

Views: 411

Answers (1)

markvasiv
markvasiv

Reputation: 522

Most probably this happens because you put AV code inside of viewWillAppear. This method executed every time you enter the view (in your case it's when the app starts and when the user gets back to the main menu).

This line definitely isn't meant to be executed multiple times:

try session.addInput(AVCaptureDeviceInput(device: device))

And this one:

session.startRunning()

And this:

self.view.insertSubview(blurView,at:1)

A quick fix would be to put all of this logic in a dedicated function and add a flag to run it only once. Something like that:

override func viewWillAppear(_ animated: Bool) {
   super.viewWillAppear(animated)
   addBlur()
}

var didAddBlur = false
func addBlur() {
   if didAddBlur {
       return
   }

   didAddBlur = true

   //Your code
}

Upvotes: 1

Related Questions