简体   繁体   English

当我返回菜单时,应用程序崩溃(线程 1:信号 SIGABRT)

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

I am almost finished with my App "AR Note".我几乎完成了我的应用程序“AR Note”。 There is only one bug I need to fix.我只需要修复一个错误。

I have a main menu with 7 buttons linking to view controllers.我有一个主菜单,其中有 7 个按钮链接到视图控制器。 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!现在,有了这个模糊相机视图背景,应用程序总是崩溃,线程 1:信号 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:这是我的主菜单的样子:

图片1

This is how my main.storyboard looks like:这是我的 main.storyboard 的样子:

图像2

This is how I use the back-buttons:这就是我使用后退按钮的方式:

图像3

This is an alternative viewcontroller.swift , without the blur stuff, which works perfectly fine:这是一个替代viewcontroller.swift ,没有模糊的东西,它工作得很好:

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图 4

There is this:有这个:

image5图像5

and this:和这个:

image6图 6

Most probably this happens because you put AV code inside of viewWillAppear .发生这种情况很可能是因为您将 AV 代码放在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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM