Swift - 使用 ZoomFactor 捕获 RAW 图像会使应用程序崩溃

问题描述 投票:0回答:2

我在使用与

zoomFactor
不同的
1.0
拍摄 RAW 照片时遇到问题。 事实上,如果我用最小缩放级别拍摄照片,一切都会正常。但是,如果我尝试拉近某个主题并更改
zoomFactor
,则应用程序会崩溃并出现以下错误:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** -[AVCapturePhotoOutput capturePhotoWithSettings:delegate:] 指定Bayer raw capture时,视频设备的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()
        }
    }
}
ios swift avfoundation avcapturesession
2个回答
0
投票

请检查设备支持的缩放系数,其中

captureDevice
是您的
AVCaptureDevice
实例:

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

0
投票

我和你有同样的问题。似乎您无法在不支持 Apple ProRAW 的设备上指定比例因子。如果您的设备支持 ProRAW,那么使用比例因子就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.