App crashing while taking both video and photo from same AVCaptureSession?
我正在尝试使用 AVFoundation 使用相同的 AVCaptureSession 制作一个包含音频、视频和照片的应用程序。
以下是我如何设置相机和捕获会话。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
func setUpCaptureSession(){ captureSession.sessionPreset = AVCaptureSession.Preset.photo } func setUpDevice(){ currentCamera = backCamera func setUpInputOutput(){ photoOutput = AVCapturePhotoOutput() if #available(iOS 11.0, *) { func setUpPreviewLayer(){ func startRunningCaptureSession(){ //Session starts with camera as default so set it to gesture |
要从相机拍照(正在工作,照片存储在图库中),我做了以下操作:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
@objc func cameraAction(sender: UIButton){
if(imageSetAction()){ let doubleTap = UITapGestureRecognizer(target: self, action: #selector(takePhoto(sender:))) } @objc func takePhoto(sender: Any){ guard let capturePhotoOutput = self.photoOutput else { } func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { guard error == nil, guard let imageData = let capturedImage = UIImage.init(data: imageData , scale: 1.0) } } |
但是当我尝试从同一个捕获会话中拍照时,我收到错误 “NSInvalidArgumentException//’,原因://’*** -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] No active/enabled connections//'”。下面是我尝试做的代码。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) { print("completed") } @objc func videoAction(sender: UIButton){ if(imageSetAction()){ captureSession.removeOutput(photoOutput!) self.movieFileOutput = AVCaptureMovieFileOutput() let longPressGesture = UILongPressGestureRecognizer.init(target: self, action: #selector(handleLongPress)) @objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) { if gestureRecognizer.state == UIGestureRecognizerState.began { |
对此的任何帮助表示赞赏。
您可以尝试像这样更改 videoAction 中的预设
1
|
captureSession.sessionPreset = AVCaptureSessionPresetHigh
|
你可以在这里看到类似的问题
AVCaptureMovieFileOutput – 没有活动/启用的连接
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268615.html