代码中的错误,其中按钮并不总是执行操作

时间:2017-07-21 06:21:44

标签: ios swift xcode debugging swift3

我的应用程序中有一个按钮,有些奇怪的时刻,当我点击按钮时,它并不总是执行所需的代码。该应用程序不会崩溃,但如果我退出应用程序并再次打开它按钮将继续不起作用。解决问题的唯一方法是关闭手机或删除并重新下载应用程序。

注意:这只会偶尔发生一次,我不知道为什么。问题可能是一个可能的约束问题吗?这是我按钮的代码。

@IBAction func recordVideoButtonPressed(_ sender: Any) {

    if self.movieFileOutput.isRecording {
        clipsCollectionView.reloadData()
        self.movieFileOutput.stopRecording()
        styleWhileNotRecording()
        turnFlashOff()
        stopTimer()
    } else {
        self.movieFileOutput.connection(withMediaType: AVMediaTypeVideo).videoOrientation = self.videoOrientation()
        self.movieFileOutput.maxRecordedDuration = self.maxRecordedDuration()
        self.movieFileOutput.startRecording(toOutputFileURL: URL(fileURLWithPath:self.videoFileLocation()), recordingDelegate: self)
        styleWhileRecording()
        turnOnFlash()
        startTimer()
    }

    updateRecordButtonTitle()

}
  

这是我的updateRecordButton方法。这就是我所知道的   问题,因为图像并不总是更新。

func updateRecordButtonTitle() {
    recordButton.alpha = 0
    if !self.movieFileOutput.isRecording {
        UIView.animate(withDuration: 0.3, animations: {
        let stop = UIImage(named: "stopButton.png")
        self.recordButton.setImage(stop, for: .normal)
        self.recordButton.alpha = 1
            })
    } else {
        UIView.animate(withDuration: 0.3, animations: {
        let start = UIImage(named: "recordImage.png")
        self.recordButton.setImage(start, for: .normal)
        self.recordButton.alpha = 1
         })
    }

}
  

UPDATE !!

我刚注意到,当出现此问题时,计时器也不会停止。所以我假设问题是它没有正确读取。任何人都有任何关于为什么会这样的想法?

if self.movieFileOutput.isRecording {
  

更新

我刚刚意识到另一个问题。我在委托捕获方法中添加了print语句。

    func capture(_ captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAt fileURL: URL!, fromConnections connections: [Any]!) {
    print(42332)
}

func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
    cropVideo(outputFileURL)
    print("Recording Finished")
}

发生此问题时...将打印所有这些语句。它打印

42322并完成录制。

我意识到它发生了什么,当这发生时,它会记录视频并立即停止。我不确定为什么会发生这种情况????

1 个答案:

答案 0 :(得分:0)



 let cameraButton: UIButton = {
        let cameraButton = UIButton(type: .system)
        cameraButton.setImage(#imageLiteral(resourceName: "camera").withRenderingMode(.alwaysOriginal), for: .normal)
        cameraButton.translatesAutoresizingMaskIntoConstraints = false
        return cameraButton
    }()


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

func addButtons() {
        self.view.addSubview(cameraButton)
        cameraButton.bottomAnchor.constraint(equalTo: self.view.topAnchor, constant: self.view.frame.height*3/6).isActive = true
        cameraButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        cameraButton.heightAnchor.constraint(equalToConstant: 190).isActive = true
        cameraButton.widthAnchor.constraint(equalToConstant: 190).isActive = true
        cameraButton.addTarget(self, action: #selector(cameraTapped), for: .touchUpInside)
        }
        
 func cameraTapped() {
        let videoPickerController = UIImagePickerController()
        videoPickerController.delegate = self
        
        if UIImagePickerController.isSourceTypeAvailable(.camera) == false {
           return
        }
        videoPickerController.allowsEditing = true
        videoPickerController.sourceType = .camera
        videoPickerController.mediaTypes = [kUTTypeMovie as String]
        videoPickerController.modalPresentationStyle = .fullScreen
        self.present(videoPickerController, animated: true, completion: nil)
    }
    
    // After picking a video, we dismiss the picker view controller and present the editor view controller
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let videoURL = info["UIImagePickerControllerMediaURL"] as? URL
        self.dismiss(animated: true, completion: nil)
        
        // We create a VideoEditorViewController to play video as well as for editing purpose
        let videoEditorViewController = VideoEditorViewController()
        videoEditorViewController.videoURL = videoURL
        videoEditorViewController.transitioningDelegate = self
        self.present(videoEditorViewController, animated: true, completion: nil)
    }
}