尝试删除覆盖时相机捕获崩溃

Camera capture crash when trying to remove overlay

我声明:

let image = UIImagePickerController()

然后设置:

image.delegate = self
image.sourceType = .Camera
image.cameraDevice = .Front
image.allowsEditing = false

然后向相机添加叠加层:

let overlay = self.storyboard?.instantiateViewControllerWithIdentifier("OverlayVC")
image.cameraOverlayView = overlay?.view

然后出示相机:

self.presentViewController(image, animated: true, completion: nil)

然后我拍了张照片,这时我的观察员开始了:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "removeOverlay", name: "_UIImagePickerControllerUserDidCaptureItem", object: nil)

并尝试执行:

func removeOverlay() {
        image.cameraOverlayView = nil
    }

现在一切正常,大部分时间都删除了叠加层,但在随机情况下,应用程序崩溃:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type must be UIImagePickerControllerSourceTypeCamera'

我的猜测是,这与我执行删除覆盖命令的时间有关,但不知道如何从这里开始。 有什么见解吗?

通过添加检查修复了它:

if image.cameraOverlayView != nil { 
    image.cameraOverlayView = nil
}