Swift:关闭VC时视图(AVCaptureVideoPreviewLayer)是否冻结?

时间:2018-11-27 02:51:28

标签: ios iphone swift xcode

我有一个类似于Snapchat的基本相机应用程序,当用户拍照时,他们可以预览并看到图像。完成后,可以按下一个按钮,将VC撤消,然后将dgo返回相机。效果很好,但是当用户拍摄视频然后预览时,然后按取消按钮并关闭VC时,可以在其中拍摄照片的“视图”就会冻结。

为解决此问题,我本来是做一个Segue而不是解雇。但是我发现这会使取消功能非常慢。所以我的问题是如何解决这个死机问题?

下面是我当前如何从PreviewVC过渡:

@IBAction func cancelButton(_ sender: UIButton) {
    dismiss(animated: false, completion: nil)
    //deletes video data
    self.playerQueue?.removeAllItems()
}

可以找到下面的预览VC,其中显示了视频和照片

    class PreviewViewController: UIViewController {

    @IBOutlet weak var mediaView: UIImageView!

    var image: UIImage!

    //To hide status bar:
    //    var statusBarHidden : Bool?

    override func viewDidLoad() {
        super.viewDidLoad()

        print(self.image)
        mediaView.image = self.image

        //bellow is for vid

        if videoURL != nil {
            print("videoURL", videoURL)

            playerItem1 = AVPlayerItem(url: videoURL as URL)
            print(playerItem1, "playerItem1")

            playerQueue = AVQueuePlayer(playerItem: playerItem1)
            print(playerQueue, "player queue")

            playerLayer = AVPlayerLayer(player: playerQueue)
            print(playerLayer, "playerLayer")

            playerLooper = AVPlayerLooper(player: playerQueue, templateItem: playerItem1)
            print(playerLooper, "player looper")

            playerLayer.frame = view.bounds
            playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
            print(mediaView, "yruewioq")
            mediaView.layer.insertSublayer(playerLayer, at: 0)

            view.layoutIfNeeded()
            self.playerQueue?.play()
        }
    }


    //TO SAVE:
    @IBAction func saveButton(_ sender: UIButton) {
        if videoURL == nil {
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
            dismiss(animated: false, completion: nil)
        }
    }

    //TO CANCEL:
    @IBAction func cancelButton(_ sender: UIButton) {
    dismiss(animated: false, completion: nil)
    }

    //receives url video value from VideoViewController
    var videoURL: URL!

    private var playerQueue: AVQueuePlayer!
    private var playerItem1: AVPlayerItem!
    private var playerLooper: AVPlayerLooper!
    private var playerLayer: AVPlayerLayer!        

}

以下是我在mainVC中的viewDidLoad()

    override func viewDidLoad() {
    super.viewDidLoad()
    print("67423814700000000")
    //INTEGRATION nov 24 (putting nside if espcially is inTEGRAIO)
    if setupInputOutput() {
        print("in this gfgf")
        setupCaptureSession()
        setupDevice()
        //setupInputOutput()
        //            setupPreviewLayer()
        startRunningCaptureSession()
    }
    setupPreviewLayer()


    //INTEGRATED below nov 24 START =-=-=-=-=-
    //for button ---||||||||
    cameraButton.isUserInteractionEnabled = true

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
    tapGesture.numberOfTapsRequired = 1
    cameraButton.addGestureRecognizer(tapGesture)

    let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
    longGesture.minimumPressDuration = 0.15
    //longGesture.allowableMovement = 100

    //this functionality below may be imp?
    //        longGesture.delaysTouchesBegan
    cameraButton.addGestureRecognizer(longGesture)

    camPreview.addSubview(cameraButton)
    //INTEGRATED below nov 24 END =-=-=-=-=-

}

更新:它冻结在用户放开captureButton时捕获的帧。

2 个答案:

答案 0 :(得分:0)

您似乎正在一层中运行视频,然后为解雇动画设置了动画。可能是在播放视频时对其进行动画处理的原因,这会导致速度变慢,因此请在删除mediaView.layer.sublayers = nil之前完全删除AVPlayerLayer,暂停它,或者拍摄快照,然后用快照替换该图层。 / p>

答案 1 :(得分:0)

我已经能够解决问题,但是,更改现在导致从摄像机到PreviewVC的初始过渡变慢(此要求优化)。我通过

解决了问题
override func viewDidDisappear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("")
    //setupPreviewLayer() //Call the setup of the camera here so that if the user enters the view controller from another view controller, the camera is established. cud also be: setupInputOutput()

    DispatchQueue.global().async {
        self.startRunningCaptureSession() //perhaps this REALLY SHUD BE IN LIKE VIEW WILL DISAPPEAR OR SUM OTHER THING perhaps?
    }
}

在mainVC中。