如何解决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
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。