简体   繁体   English

尝试删除叠加层时相机捕获崩溃

[英]Camera capture crash when trying to remove overlay

I am declaring: 我在声明:

let image = UIImagePickerController()

then setting: 然后设置:

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

then add an overlay to camera: 然后将叠加层添加到相机:

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

and then presenting camera: 然后展示相机:

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

then I take a picture, at which point my observer kicks in: 然后我拍照,这时我的观察者踢了进来:

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

and tries to execute: 并尝试执行:

func removeOverlay() {
        image.cameraOverlayView = nil
    }

Now all works well and overlay is removed most of the time but on random occasions, app crashes: 现在,一切正常,大部分时间都删除了重叠式广告,但是在随机情况下,应用崩溃了:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type must be UIImagePickerControllerSourceTypeCamera' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“源类型必须为UIImagePickerControllerSourceTypeCamera”

My guess is that it is something to do with the timing of when my remove overlay command is executed but do not know how to proceed from here. 我的猜测是,这与执行我的remove overlay命令的时间有关,但不知道如何从此处进行。 Any insight? 有见识吗?

Fixed it by adding a check: 通过添加检查来修复它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM