macOS swift AVCaptureSession sessionPreset不起作用

时间:2018-05-29 04:00:36

标签: ios swift macos avfoundation avcapturesession

我正在使用AVFoundation拍摄照片。但无论如何我改变了sessionPreset,输出仍为videoDevice.activeFormat Optional(<AVCaptureDeviceFormat: 0x608000001eb0> 'vide'/'2vuy' enc dims = 1280x720, pres dims = 1280x720 { 29.97 25.00 24.00 15.00 fps })

我想要的只是将输出更改为高质量。

我的整个代码是

    self.avCaptureSession = AVCaptureSession()

    let devices = AVCaptureDevice.devices()
    self.avCurrentCaptureDevice = devices!.last as? AVCaptureDevice
    do {
        self.avCaptureInput = try AVCaptureDeviceInput(device: self.avCurrentCaptureDevice)
    } catch {
    }

    do {
        try self.avCurrentCaptureDevice?.lockForConfiguration()
    } catch {
        return
    }

    self.avCaptureSession?.beginConfiguration()
    self.avCaptureSession?.addInput(self.avCaptureInput)

    self.avCaptureSession?.sessionPreset = AVCaptureSessionPreset320x240

    self.avOutput = AVCaptureVideoDataOutput()
    if (self.avCaptureSession?.canAddOutput(self.avOutput) != nil) {
        self.avCaptureSession?.addOutput(self.avOutput)
    }
    self.avOutput?.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString) : NSNumber(value: kCVPixelFormatType_32BGRA as UInt32)]
    self.avOutput?.alwaysDiscardsLateVideoFrames = true

    let queue: DispatchQueue = DispatchQueue(label: "myqueue",  attributes: [])
    self.avOutput?.setSampleBufferDelegate(self, queue: queue)

    self.avCaptureSession?.commitConfiguration()

    print("videoDevice.activeFormat", self.avCurrentCaptureDevice?.activeFormat! as Any)

1 个答案:

答案 0 :(得分:0)

捕获输入和捕获输出格式可以不同。

首先尝试检查输入设备的最大尺寸和fps。 我想预设是basiccaly应用于输出格式。

此外,我的代码可能是您的帮助。 AVCaptureManager