AVFoundation 错误代码
AVFoundation Error Codes
我在拍摄 RAW 和 JPEG 照片时收到以下两条错误消息。我无法诊断这些:
Error capturing photo: Error Domain=AVFoundationError- Domain
Code=-11800 "The operation could not be completed"
Userlnfo={NSLocalizedFailur- eReason=An unknown erroroccurred (42686),
NSLocalizedDescription:The operation could not be completed,
NSUnderlyingEr- ror=0x1c804bfa0 {Error Domain=NSOSStatusError- Domain
Code=-12686 "(null)"}}
还有...
Error capturing photo: Error Domain=AVFoundationError-Domain
Code=-11800 The operation could not be completed"
UserInfo.{NSLocalized-FailureReason=An unknown error occurred
(-16802), NSLocalizedDescription=The operation could not be completed,
NSUnderlyingEr-ror=0x1c4243f30 {Error Domain=NSOSStatusError-Domain
Code=-16802 "(null)"}}
这似乎发生在 AVCapturePhotoCaptureDelegate 中的这个函数之后:
optional func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?)
这些似乎是在重复拍摄照片时随机发生的。
更新
RAW 设置
func updatePhotoSettings () {
self.photoOutput.setPreparedPhotoSettingsArray([])
let newPhotoSettings = AVCapturePhotoSettings(rawPixelFormatType: OSType(self.photoOutput.availableRawPhotoPixelFormatTypes.first!),
processedFormat: [AVVideoCodecKey : AVVideoCodecJPEG])
newPhotoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: self.photoOutput.availablePhotoPixelFormatTypes[0]]
if #available(iOS 11.0, *) {
newPhotoSettings.embeddedThumbnailPhotoFormat = [AVVideoCodecKey: self.photoOutput.availablePhotoCodecTypes[0]]
}
newPhotoSettings.isHighResolutionPhotoEnabled = true
newPhotoSettings.flashMode = self.flashMode
newPhotoSettings.isAutoStillImageStabilizationEnabled = false
newPhotoSettings.livePhotoMovieFileURL = nil
self.photoOutput.photoSettingsForSceneMonitoring = newPhotoSettings
self.photoOutput.setPreparedPhotoSettingsArray([newPhotoSettings])
}
捕获调用:
final func snapImage () {
let photoCaptureDelegate = PhotoCaptureDelegate(with: self.photoOutput.preparedPhotoSettingsArray.first!,
willCapturePhotoAnimation: {
//Shutter animation
DispatchQueue.main.async { [unowned self] in
self.previewView.videoPreviewLayer.opacity = 0
UIView.animate(withDuration: 0.1, animations: {
self.previewView.videoPreviewLayer.opacity = 1
})
}
}, didCapturePhoto: {
//Photo Saved animation
}, completed: { [unowned self] photoCaptureDelegate in
self.inProgressPhotoCaptureDelegates[photoCaptureDelegate.requestedPhotoSettings.uniqueID] = nil
})
self.inProgressPhotoCaptureDelegates[photoCaptureDelegate.requestedPhotoSettings.uniqueID] = photoCaptureDelegate
self.photoOutput.capturePhoto(with: self.photoOutput.preparedPhotoSettingsArray.first!, delegate: photoCaptureDelegate)
DispatchQueue.main.async {
//Goes to update photo settings for next shot
self.updatePhotoSettings()
}
}
}
照片拍摄代表
import AVFoundation
import Photos
import MobileCoreServices
class PhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate {
private(set) var requestedPhotoSettings : AVCapturePhotoSettings
private let willCapturePhotoAnimation : () -> ()
private let didCapturePhoto : () -> ()
private let completed : (PhotoCaptureDelegate) -> ()
private var jpegPhotoData : Data?
private var dngPhotoData : Data?
// MARK: - Initialization
init(with requestedPhotoSettings: AVCapturePhotoSettings,
willCapturePhotoAnimation: @escaping () -> (),
didCapturePhoto: @escaping () -> (),
completed: @escaping (PhotoCaptureDelegate) -> ())
{
self.requestedPhotoSettings = requestedPhotoSettings
self.willCapturePhotoAnimation = willCapturePhotoAnimation
self.didCapturePhoto = didCapturePhoto
self.completed = completed
}
private final func didFinish() {
self.completed(self)
}
// MARK: - Will Capture
final func capture(_ captureOutput: AVCapturePhotoOutput, willCapturePhotoForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings) {
willCapturePhotoAnimation()
}
final func capture(_ output: AVCapturePhotoOutput, willBeginCaptureForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings) {
}
// MARK: - iOS 11 Did Finish Photo
@available(iOS 11.0, *)
final func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let error = error {
PhotoCaptureDelegate.showError(sentMessage: "Error processing photo: \(error)")
self.didCapturePhoto()
} else {
if photo.isRawPhoto {
self.dngPhotoData = photo.fileDataRepresentation()
return
} else {
if self.jpegPhotoData == nil {
self.jpegPhotoData = photo.fileDataRepresentation()
self.didCapturePhoto()
return
}
}
}
}
// MARK: - Did Finish Capture
final func capture(_ captureOutput: AVCapturePhotoOutput, didFinishCaptureForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings, error: Error?) {
if let error = error {
PhotoCaptureDelegate.showError(sentMessage: "Error capturing photo: \(error)")
didFinish()
return
if PHPhotoLibrary.authorizationStatus() == .authorized {
var temporaryDNGFileURL : URL? = nil
self.dngPhotoData != nil {
temporaryDNGFileURL = URL(fileURLWithPath: NSTemporaryDirectory().appending("\(resolvedSettings.uniqueID).dng"))
do {
try self.dngPhotoData!.write(to: temporaryDNGFileURL!, options: [.atomic])
} catch let error as NSError {
PhotoCaptureDelegate.showError(sentMessage: "Could not write DNG Data: \(error)")
return
}
}
PHPhotoLibrary.shared().performChanges({
let creationRequest = PHAssetCreationRequest.forAsset()
if (self.jpegPhotoData != nil) {
creationRequest.addResource(with: .photo, data: self.jpegPhotoData!, options: nil)
if let temporaryDNGFileURL = temporaryDNGFileURL {
let companionDNGResourceOptions = PHAssetResourceCreationOptions()
companionDNGResourceOptions.shouldMoveFile = true
creationRequest.addResource(with: .alternatePhoto, fileURL: temporaryDNGFileURL, options: companionDNGResourceOptions)
}
}
}, completionHandler: { [unowned self] success, error in
if let error = error {
PhotoCaptureDelegate.showError(sentMessage: "Error occurered while saving photo to photo library: \(error)")
}
if (temporaryDNGFileURL != nil) {
if FileManager.default.fileExists(atPath: temporaryDNGFileURL!.path) {
do {
try FileManager.default.removeItem(at: temporaryDNGFileURL!)
} catch let error as NSError {
PhotoCaptureDelegate.showError(sentMessage: "Could not remove DNG File: \(error)")
}
}
}
})
}
else {
PhotoCaptureDelegate.showError(sentMessage: "Not authorized to save photo")
self.didFinish()
return
}
}
private static func showError (sentMessage: String) {
let alertController = UIAlertController(title: nil, message: sentMessage, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: NSLocalizedString("OK", comment: "Alert button title."), style: UIAlertActionStyle.cancel, handler: nil)
alertController.addAction(cancelAction)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window!.rootViewController!.present(alertController, animated: true, completion: nil)
}
}
注意事项
- Apple 的 AVCam 示例代码紧随其后。
- 此崩溃仅在更成功的照片拍摄之一之后发生。生成的 RAW 文件非常好。
更新
您在图像捕获完成之前调用 updatePhotoSettings()
,这会导致错误。尝试从完成处理程序或在拍摄图像之前执行此操作。
p.s。真的 AVFoundation 应该让这个错误更难犯或者至少更清楚这是行不通的。
之前的错误猜测
您没有显示如何配置 AVCaptureSession
这很重要,但是
如果您启用 AVCapturePhotoSettings
isHighResolutionPhotoEnabled
,您还需要启用 isHighResolutionCaptureEnabled
(对您来说可能是 highResolutionCaptureEnabled
- [= 是什么版本49=] 你在用吗?):
self.photoOutput.isHighResolutionCaptureEnabled = true
您的 PhotoCaptureDelegate
看起来超出了范围,这无济于事。把它赋值给一个成员变量,可以延长它的寿命。
在 AVCaptureSession
上设置 .photo
预设
我在拍摄 RAW 和 JPEG 照片时收到以下两条错误消息。我无法诊断这些:
Error capturing photo: Error Domain=AVFoundationError- Domain Code=-11800 "The operation could not be completed" Userlnfo={NSLocalizedFailur- eReason=An unknown erroroccurred (42686), NSLocalizedDescription:The operation could not be completed, NSUnderlyingEr- ror=0x1c804bfa0 {Error Domain=NSOSStatusError- Domain Code=-12686 "(null)"}}
还有...
Error capturing photo: Error Domain=AVFoundationError-Domain Code=-11800 The operation could not be completed" UserInfo.{NSLocalized-FailureReason=An unknown error occurred (-16802), NSLocalizedDescription=The operation could not be completed, NSUnderlyingEr-ror=0x1c4243f30 {Error Domain=NSOSStatusError-Domain Code=-16802 "(null)"}}
这似乎发生在 AVCapturePhotoCaptureDelegate 中的这个函数之后:
optional func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?)
这些似乎是在重复拍摄照片时随机发生的。
更新
RAW 设置
func updatePhotoSettings () {
self.photoOutput.setPreparedPhotoSettingsArray([])
let newPhotoSettings = AVCapturePhotoSettings(rawPixelFormatType: OSType(self.photoOutput.availableRawPhotoPixelFormatTypes.first!),
processedFormat: [AVVideoCodecKey : AVVideoCodecJPEG])
newPhotoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: self.photoOutput.availablePhotoPixelFormatTypes[0]]
if #available(iOS 11.0, *) {
newPhotoSettings.embeddedThumbnailPhotoFormat = [AVVideoCodecKey: self.photoOutput.availablePhotoCodecTypes[0]]
}
newPhotoSettings.isHighResolutionPhotoEnabled = true
newPhotoSettings.flashMode = self.flashMode
newPhotoSettings.isAutoStillImageStabilizationEnabled = false
newPhotoSettings.livePhotoMovieFileURL = nil
self.photoOutput.photoSettingsForSceneMonitoring = newPhotoSettings
self.photoOutput.setPreparedPhotoSettingsArray([newPhotoSettings])
}
捕获调用:
final func snapImage () {
let photoCaptureDelegate = PhotoCaptureDelegate(with: self.photoOutput.preparedPhotoSettingsArray.first!,
willCapturePhotoAnimation: {
//Shutter animation
DispatchQueue.main.async { [unowned self] in
self.previewView.videoPreviewLayer.opacity = 0
UIView.animate(withDuration: 0.1, animations: {
self.previewView.videoPreviewLayer.opacity = 1
})
}
}, didCapturePhoto: {
//Photo Saved animation
}, completed: { [unowned self] photoCaptureDelegate in
self.inProgressPhotoCaptureDelegates[photoCaptureDelegate.requestedPhotoSettings.uniqueID] = nil
})
self.inProgressPhotoCaptureDelegates[photoCaptureDelegate.requestedPhotoSettings.uniqueID] = photoCaptureDelegate
self.photoOutput.capturePhoto(with: self.photoOutput.preparedPhotoSettingsArray.first!, delegate: photoCaptureDelegate)
DispatchQueue.main.async {
//Goes to update photo settings for next shot
self.updatePhotoSettings()
}
}
}
照片拍摄代表
import AVFoundation
import Photos
import MobileCoreServices
class PhotoCaptureDelegate: NSObject, AVCapturePhotoCaptureDelegate {
private(set) var requestedPhotoSettings : AVCapturePhotoSettings
private let willCapturePhotoAnimation : () -> ()
private let didCapturePhoto : () -> ()
private let completed : (PhotoCaptureDelegate) -> ()
private var jpegPhotoData : Data?
private var dngPhotoData : Data?
// MARK: - Initialization
init(with requestedPhotoSettings: AVCapturePhotoSettings,
willCapturePhotoAnimation: @escaping () -> (),
didCapturePhoto: @escaping () -> (),
completed: @escaping (PhotoCaptureDelegate) -> ())
{
self.requestedPhotoSettings = requestedPhotoSettings
self.willCapturePhotoAnimation = willCapturePhotoAnimation
self.didCapturePhoto = didCapturePhoto
self.completed = completed
}
private final func didFinish() {
self.completed(self)
}
// MARK: - Will Capture
final func capture(_ captureOutput: AVCapturePhotoOutput, willCapturePhotoForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings) {
willCapturePhotoAnimation()
}
final func capture(_ output: AVCapturePhotoOutput, willBeginCaptureForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings) {
}
// MARK: - iOS 11 Did Finish Photo
@available(iOS 11.0, *)
final func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let error = error {
PhotoCaptureDelegate.showError(sentMessage: "Error processing photo: \(error)")
self.didCapturePhoto()
} else {
if photo.isRawPhoto {
self.dngPhotoData = photo.fileDataRepresentation()
return
} else {
if self.jpegPhotoData == nil {
self.jpegPhotoData = photo.fileDataRepresentation()
self.didCapturePhoto()
return
}
}
}
}
// MARK: - Did Finish Capture
final func capture(_ captureOutput: AVCapturePhotoOutput, didFinishCaptureForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings, error: Error?) {
if let error = error {
PhotoCaptureDelegate.showError(sentMessage: "Error capturing photo: \(error)")
didFinish()
return
if PHPhotoLibrary.authorizationStatus() == .authorized {
var temporaryDNGFileURL : URL? = nil
self.dngPhotoData != nil {
temporaryDNGFileURL = URL(fileURLWithPath: NSTemporaryDirectory().appending("\(resolvedSettings.uniqueID).dng"))
do {
try self.dngPhotoData!.write(to: temporaryDNGFileURL!, options: [.atomic])
} catch let error as NSError {
PhotoCaptureDelegate.showError(sentMessage: "Could not write DNG Data: \(error)")
return
}
}
PHPhotoLibrary.shared().performChanges({
let creationRequest = PHAssetCreationRequest.forAsset()
if (self.jpegPhotoData != nil) {
creationRequest.addResource(with: .photo, data: self.jpegPhotoData!, options: nil)
if let temporaryDNGFileURL = temporaryDNGFileURL {
let companionDNGResourceOptions = PHAssetResourceCreationOptions()
companionDNGResourceOptions.shouldMoveFile = true
creationRequest.addResource(with: .alternatePhoto, fileURL: temporaryDNGFileURL, options: companionDNGResourceOptions)
}
}
}, completionHandler: { [unowned self] success, error in
if let error = error {
PhotoCaptureDelegate.showError(sentMessage: "Error occurered while saving photo to photo library: \(error)")
}
if (temporaryDNGFileURL != nil) {
if FileManager.default.fileExists(atPath: temporaryDNGFileURL!.path) {
do {
try FileManager.default.removeItem(at: temporaryDNGFileURL!)
} catch let error as NSError {
PhotoCaptureDelegate.showError(sentMessage: "Could not remove DNG File: \(error)")
}
}
}
})
}
else {
PhotoCaptureDelegate.showError(sentMessage: "Not authorized to save photo")
self.didFinish()
return
}
}
private static func showError (sentMessage: String) {
let alertController = UIAlertController(title: nil, message: sentMessage, preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: NSLocalizedString("OK", comment: "Alert button title."), style: UIAlertActionStyle.cancel, handler: nil)
alertController.addAction(cancelAction)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window!.rootViewController!.present(alertController, animated: true, completion: nil)
}
}
注意事项
- Apple 的 AVCam 示例代码紧随其后。
- 此崩溃仅在更成功的照片拍摄之一之后发生。生成的 RAW 文件非常好。
更新
您在图像捕获完成之前调用 updatePhotoSettings()
,这会导致错误。尝试从完成处理程序或在拍摄图像之前执行此操作。
p.s。真的 AVFoundation 应该让这个错误更难犯或者至少更清楚这是行不通的。
之前的错误猜测
您没有显示如何配置 AVCaptureSession
这很重要,但是
如果您启用
AVCapturePhotoSettings
isHighResolutionPhotoEnabled
,您还需要启用isHighResolutionCaptureEnabled
(对您来说可能是highResolutionCaptureEnabled
- [= 是什么版本49=] 你在用吗?):self.photoOutput.isHighResolutionCaptureEnabled = true
您的
PhotoCaptureDelegate
看起来超出了范围,这无济于事。把它赋值给一个成员变量,可以延长它的寿命。在
AVCaptureSession
上设置
.photo
预设