【发布时间】:2015-04-10 19:21:43
【问题描述】:
在客户端手机上,相机释放期间很少会出现崩溃
Fatal Exception: NSRangeException
Cannot remove an observer <AVCaptureSession 0x174212170> for the key path "changeSeed" from <AVCaptureConnection 0x17420fa60> because it is not registered as an observer.
Thread : Fatal Exception: NSRangeException
0 CoreFoundation 0x000000018449259c __exceptionPreprocess + 132
1 libobjc.A.dylib 0x0000000194be40e4 objc_exception_throw + 60
2 CoreFoundation 0x00000001844924dc -[NSException initWithCoder:]
3 Foundation 0x00000001852a7e9c -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 528
4 Foundation 0x00000001852a7954 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 104
5 AVFoundation 0x0000000182d21054 -[AVCaptureSession _removeConnection:] + 192
6 AVFoundation 0x0000000182d206dc -[AVCaptureSession _removeVideoPreviewLayer:] + 120
7 AVFoundation 0x0000000182d300f8 -[AVCaptureVideoPreviewLayer dealloc] + 92
对于停止捕获会话,使用以下代码: 所有会话操作都在后台队列中进行,因为 stopRunning 可能需要一些时间
deinit {
if let session = self.captureSession {
dispatch_async(self.cameraQueue, { () -> Void in
session.beginConfiguration()
let allInputs = session.inputs as! [AVCaptureInput]
for input in allInputs {
session.removeInput(input)
}
let allOutputs = session.outputs as! [AVCaptureOutput]
for output in allOutputs {
session.removeOutput(output)
}
session.commitConfiguration()
session.stopRunning()
})
}
}
以前有人见过这种崩溃吗?
【问题讨论】:
-
不是一个答案,但是为什么还要移除输入和输出呢?为什么不只是
stopRunning()? -
这里推荐stackoverflow.com/a/11377825/2035054,防止内存开销
标签: ios crash key-value-observing avcapturesession dealloc