Swift - 使用 zoomFactor 捕获 RAW 图像会导致应用程序崩溃

问题描述

我无法使用与 zoomFactor 不同的 1.0 拍摄 RAW 照片。 事实上,如果我用最小缩放级别拍照,一切正常。但是,如果我尝试放大到更靠近更改 zoomFactor主题,应用程序会崩溃并显示以下错误

由于未捕获的异常'NSinvalidargumentexception'而终止应用,原因:'*** -[AVCapturePhotoOutput capturePhotoWithSettings:delegate:] 当指定 Bayer 原始捕获时,视频设备的 videoZoomFactor 必须设置为 1.0'

这仅在拍摄 RAW 时发生。如果我使用标准 HEVC 格式拍摄,一切正常。我正在使用 Swift 4.2 和 AVFoundation 框架

这是错误引用的代码

extension CameraController: AVCapturePhotoCaptureDelegate,AVCaptureVideoDataOutputSampleBufferDelegate {

public func photoOutput(_ output: AVCapturePhotoOutput,didFinishProcessingPhoto photo: AVCapturePhoto,error: Error?) {
    guard error == nil else {
        print("Error capturing photo: \(error!)")
        return
    }
    
    // Access the file data representation of this photo.
    guard let photoData = photo.fileDataRepresentation() else {
        print("No photo data to write.")
        return
    }
    
    print("Generating IMAGE with Metadata \n",photo.Metadata)
    
    
    if photo.isRawPhoto {
        // Generate a unique URL to write the RAW file.
        rawFileURL = makeUniquednGFileURL()
        do {
            // Write the RAW (DNG) file data to a URL.
            try photoData.write(to: rawFileURL!)
            print("RAW-URL Generated")
            createraWImageOnAlbum(withRAWURL: rawFileURL!)
        } catch {
            fatalError("Couldn't write DNG file to the URL.")
        }
    } else {
        createHEVCPhotoOnAlbum(photo: photo)
    }
}

private func makeUniquednGFileURL() -> URL {
    let tempDir = FileManager.default.temporaryDirectory
    let fileName = ProcessInfo.processInfo.globallyUniqueString
    return tempDir.appendingPathComponent(fileName).appendingPathExtension("dng")
}

}

你知道这是什么原因吗?

我正在此处设置 zoomFactor

    func updateZoom(tovalue: CGFloat) throws {
    let session = AVCaptureDevice.discoverySession(deviceTypes: [.builtInWideAngleCamera],mediaType: AVMediaType.video,position: .unspecified)
    
    guard let cameras = (session.devices.compactMap { $0 }) as? [AVCaptureDevice],!cameras.isEmpty else { throw CameraControllerError.noCamerasAvailable }
    
    for camera in cameras {
        if camera.position == .back {
            self.rearCamera = camera
            try camera.lockForConfiguration()
            camera.ramp(toVideoZoomFactor: tovalue,withRate: 4)
            camera.unlockForConfiguration()
        } else if camera.position == .front {
            self.frontCamera = camera
            try camera.lockForConfiguration()
            camera.ramp(toVideoZoomFactor: tovalue,withRate: 4)
            camera.unlockForConfiguration()
        }
    }
}

解决方法

请检查设备的支持缩放系数,其中 captureDevice 是您的 AVCaptureDevice 实例:

func checkZoom(zoomFactor: CGFloat) {
    
    guard
        zoomFactor <= captureDevice.maxAvailableVideoZoomFactor,zoomFactor >= captureDevice.minAvailableVideoZoomFactor
    else {
        print("ZoomFactor not supported \(zoomFactor)")
        return
    }
}