NSRangeException - 无法删除观察者
NSRangeException - cannot remove observer
我正在尝试从相机捕捉图像和录制视频,但每当我用相机关闭视图时,我都会收到此错误:
Terminating app due to uncaught exception 'NSRangeException', reason:
'Cannot remove an observer App.CameraViewController 0x16ea7f70 for
the key path "movieFileOutput.recording" from
App.CameraViewController 0x16ea7f70 because it is not registered as
an observer.'
*** First throw call stack: (0x2a9e6fef 0x3908ac8b 0x2a9e6f35 0x2b6850f5 0x2b684b07 0x2b6849ab 0xbeb40 0xbca88 0x85c173 0x864d67
0x85ea61 0x866b09 0x867e19 0x39782dc1 0x39782b14) libc++abi.dylib:
terminating with uncaught exception of type NSException
我的代码:
class CameraViewController: UIViewController, CameraDelegate, AVCaptureFileOutputRecordingDelegate {
// MARK:
// MARK: - Property
@IBOutlet weak var captureButton: UIButton!
@IBOutlet var previewView: CameraPreviewView!
weak var cameraDelegate: CameraDelegate?
var sessionQueue: dispatch_queue_t?
var captureSession: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var videoDeviceInput: AVCaptureDeviceInput?
var movieFileOutput: AVCaptureMovieFileOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
var deviceAuthorized: Bool = false
var backgroundRecordId: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
var sessionRuningAndDeviceAuthorized: Bool {
get {
return (self.captureSession?.running != nil && self.deviceAuthorized )
}
}
var runtimeErrorHandlingObserver: AnyObject?
...
}
添加观察员:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
dispatch_async(self.sessionQueue!, {
// TODO: Make constants
self.addObserver(self, forKeyPath: "sessionRunningAndDeviceAuthorized", options: NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, context: &SessionRunningAndDeviceAuthorizedContext)
self.addObserver(self, forKeyPath: "stillImageOutput.capturingStillImage", options: NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, context: &CapturingStillImageContext)
self.addObserver(self, forKeyPath: "movieFileOutput.recording", options: NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, context: &RecordingContext)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "subjectAreaDidChange:", name: AVCaptureDeviceSubjectAreaDidChangeNotification, object: self.videoDeviceInput?.device)
weak var weakSelf = self
self.runtimeErrorHandlingObserver = NSNotificationCenter.defaultCenter().addObserverForName(AVCaptureSessionRuntimeErrorNotification, object: self.captureSession, queue: nil, usingBlock: {
(note: NSNotification?) in
var stronSelf: CameraViewController = weakSelf!
dispatch_async(stronSelf.sessionQueue!, {
if let sess = stronSelf.captureSession {
sess.startRunning()
}
})
})
self.captureSession?.startRunning()
})
}
以及移除观察者的代码:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
dispatch_async(self.sessionQueue!, {
if let sess = self.captureSession {
sess.stopRunning()
NSNotificationCenter.defaultCenter().removeObserver(self, name: AVCaptureDeviceSubjectAreaDidChangeNotification, object: self.videoDeviceInput?.device)
NSNotificationCenter.defaultCenter().removeObserver(self.runtimeErrorHandlingObserver!)
// TODO: Make constants
self.removeObserver(self, forKeyPath: "sessionRunningAndDeviceAuthorized", context: &SessionRunningAndDeviceAuthorizedContext)
self.removeObserver(self, forKeyPath: "stillImageOutput.capturingStillImage", context: &CapturingStillImageContext)
self.removeObserver(self, forKeyPath: "movieFileOutput.recording", context: &SessionRunningAndDeviceAuthorizedContext)
}
})
}
您在 removeObserver(...)
中使用了错误的上下文
此外,为什么要删除异步块中的观察者?
我正在尝试从相机捕捉图像和录制视频,但每当我用相机关闭视图时,我都会收到此错误:
Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer App.CameraViewController 0x16ea7f70 for the key path "movieFileOutput.recording" from App.CameraViewController 0x16ea7f70 because it is not registered as an observer.' *** First throw call stack: (0x2a9e6fef 0x3908ac8b 0x2a9e6f35 0x2b6850f5 0x2b684b07 0x2b6849ab 0xbeb40 0xbca88 0x85c173 0x864d67 0x85ea61 0x866b09 0x867e19 0x39782dc1 0x39782b14) libc++abi.dylib: terminating with uncaught exception of type NSException
我的代码:
class CameraViewController: UIViewController, CameraDelegate, AVCaptureFileOutputRecordingDelegate {
// MARK:
// MARK: - Property
@IBOutlet weak var captureButton: UIButton!
@IBOutlet var previewView: CameraPreviewView!
weak var cameraDelegate: CameraDelegate?
var sessionQueue: dispatch_queue_t?
var captureSession: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var videoDeviceInput: AVCaptureDeviceInput?
var movieFileOutput: AVCaptureMovieFileOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
var deviceAuthorized: Bool = false
var backgroundRecordId: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
var sessionRuningAndDeviceAuthorized: Bool {
get {
return (self.captureSession?.running != nil && self.deviceAuthorized )
}
}
var runtimeErrorHandlingObserver: AnyObject?
...
}
添加观察员:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
dispatch_async(self.sessionQueue!, {
// TODO: Make constants
self.addObserver(self, forKeyPath: "sessionRunningAndDeviceAuthorized", options: NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, context: &SessionRunningAndDeviceAuthorizedContext)
self.addObserver(self, forKeyPath: "stillImageOutput.capturingStillImage", options: NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, context: &CapturingStillImageContext)
self.addObserver(self, forKeyPath: "movieFileOutput.recording", options: NSKeyValueObservingOptions.Old | NSKeyValueObservingOptions.New, context: &RecordingContext)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "subjectAreaDidChange:", name: AVCaptureDeviceSubjectAreaDidChangeNotification, object: self.videoDeviceInput?.device)
weak var weakSelf = self
self.runtimeErrorHandlingObserver = NSNotificationCenter.defaultCenter().addObserverForName(AVCaptureSessionRuntimeErrorNotification, object: self.captureSession, queue: nil, usingBlock: {
(note: NSNotification?) in
var stronSelf: CameraViewController = weakSelf!
dispatch_async(stronSelf.sessionQueue!, {
if let sess = stronSelf.captureSession {
sess.startRunning()
}
})
})
self.captureSession?.startRunning()
})
}
以及移除观察者的代码:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
dispatch_async(self.sessionQueue!, {
if let sess = self.captureSession {
sess.stopRunning()
NSNotificationCenter.defaultCenter().removeObserver(self, name: AVCaptureDeviceSubjectAreaDidChangeNotification, object: self.videoDeviceInput?.device)
NSNotificationCenter.defaultCenter().removeObserver(self.runtimeErrorHandlingObserver!)
// TODO: Make constants
self.removeObserver(self, forKeyPath: "sessionRunningAndDeviceAuthorized", context: &SessionRunningAndDeviceAuthorizedContext)
self.removeObserver(self, forKeyPath: "stillImageOutput.capturingStillImage", context: &CapturingStillImageContext)
self.removeObserver(self, forKeyPath: "movieFileOutput.recording", context: &SessionRunningAndDeviceAuthorizedContext)
}
})
}
您在 removeObserver(...)
此外,为什么要删除异步块中的观察者?