为什么调用.capturephoto(与:,委派:)在avCapturePhotoOutput上造成崩溃



我一直在为此错误而苦苦挣扎,几天,如果我拍摄图片segue屏幕,请解散当前的屏幕翻转相机并拍摄照片,应用程序崩溃了。blockquote>

[avcapturephotput capturephotowithetsettings:委托:]没有活动和启用的视频连接'

我将错误缩小到三行代码,尽管我不确定如何修复它,但我相信该解决方案与我的AVCapturePhotoSettings

有关

这是我将故障代码缩小到以下位置的地方:

@IBAction func cameraButton_TouchUpInside(_ sender: Any) {
    let settings = AVCapturePhotoSettings()
    photoOutPut?.capturePhoto(with: settings, delegate: self as AVCapturePhotoCaptureDelegate)
}

我是否错误地设置了设置。谢谢你

这是整个代码

import UIKit
import AVFoundation

protocol previewSegueDelegate {
func previewSegueDelegate(image:UIImage,device:AVCaptureDevice)
}
class MainCameraCollectionViewCell: UICollectionViewCell {
var gdelegate: gestureDelegate?
var pdelegate: previewSegueDelegate?

@IBOutlet weak var myView: UIView!
var captureSession = AVCaptureSession()
private var sessionQueue: DispatchQueue!
var captureConnection = AVCaptureConnection()
var currentCamera: AVCaptureDevice?
var photoOutPut: AVCapturePhotoOutput?
var cameraPreviewLayer: AVCaptureVideoPreviewLayer?
var image: UIImage?
 var usingFrontCamera = false
override func awakeFromNib() {
    super.awakeFromNib()
    setupCaptureSession()
    setupDevice()
    setupInput()
    setupPreviewLayer()
    startRunningCaptureSession()
    print("Inside of camera cell")
}
func setupCaptureSession(){
    captureSession.sessionPreset = AVCaptureSession.Preset.photo
    sessionQueue = DispatchQueue(label: "session queue")
}
func setupDevice(usingFrontCamera:Bool = false){
    sessionQueue.async {
    let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
    let devices = deviceDiscoverySession.devices
    for device in devices{
        if usingFrontCamera && device.position == AVCaptureDevice.Position.front {
            self.currentCamera = device
        } else if device.position == AVCaptureDevice.Position.back {
            self.currentCamera = device
        }
    }
    }
}
func setupInput() {
    sessionQueue.async {
        do {
            let captureDeviceInput = try AVCaptureDeviceInput(device: self.currentCamera!)
            if self.captureSession.canAddInput(captureDeviceInput) {
                self.captureSession.addInput(captureDeviceInput)
            }
            self.photoOutPut = AVCapturePhotoOutput()
            self.photoOutPut?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
            if self.captureSession.canAddOutput(self.photoOutPut!) {
                self.captureSession.addOutput(self.photoOutPut!)
            }
        } catch {
            print(error)
        }
    }
}
func setupPreviewLayer(){
    cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
    cameraPreviewLayer?.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
    self.layer.insertSublayer(cameraPreviewLayer!, at: 0)
}
func startRunningCaptureSession(){
    captureSession.startRunning()
}

@IBAction func cameraButton_TouchUpInside(_ sender: Any) {
    let settings = AVCapturePhotoSettings()
    photoOutPut?.capturePhoto(with: settings, delegate: self as AVCapturePhotoCaptureDelegate)
    print("camera button touched")
}
@IBAction func FlipThe_camera(_ sender: UIButton) {
print("Flip Touched")
    captureSession.beginConfiguration()
    if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] {
        for input in inputs {
            captureSession.removeInput(input)
        }
    }
    usingFrontCamera = !usingFrontCamera
    setupCaptureSession()
    setupDevice(usingFrontCamera: usingFrontCamera)
    setupInput()
    captureSession.commitConfiguration()
    startRunningCaptureSession()
}
}

 extension MainCameraCollectionViewCell: AVCapturePhotoCaptureDelegate{
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    if let imageData = photo.fileDataRepresentation(){
        print(imageData)
        image = UIImage(data: imageData)
        if(self.image == nil){
            print("The image is empty")
        }
        pdelegate?.previewSegueDelegate(image: self.image!, device: currentCamera!)
    }
}
}

相机开关的逻辑是错误的。

@IBAction func FlipThe_camera(_ sender: UIButton) {
     print("Flip Touched")
    captureSession.beginConfiguration()
    if let inputs = captureSession.inputs as? [AVCaptureDeviceInput] {
        for input in inputs {
            captureSession.removeInput(input)
        }
    }
    usingFrontCamera = !usingFrontCamera
    setupCaptureSession()
    setupDevice(usingFrontCamera: usingFrontCamera)
    setupInput()
    captureSession.commitConfiguration()
    startRunningCaptureSession()
}

您不应在setupInput()中再次添加输出。并且不应在startRunningCaptureSession()

中再次开始运行会话

相关内容

  • 没有找到相关文章

最新更新