Thursday, 29 November 2018

Swift: View freezing when dismissing VC?

I have a basic camera app similar to snapchats, when a user takes a picture they go to the preview and can see the image. When they are done they can press a button wich will dissmiss the VC an dgo back to the camera. This works fine, however when a user takes a video then previews it, then when they press the cancel button, dissmissing the VC, the View where they can take pictures becomes frozen.

To fix this issue I had originaly done a segue instead of a dissmiss. However I found this to make the cancel functionality very slow. SO my question is how can I resolve this freezing problem?

Below is how I currently transition out of my previewVC:

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

Bellow can be found the previewVC where Vids and photos are displayed

    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!        

}

Bellow is my viewDidLoad() in teh mainVC

    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 =-=-=-=-=-

}



from Swift: View freezing when dismissing VC?

No comments:

Post a Comment