简体   繁体   中英

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'

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. Any insight?

Fixed it by adding a check:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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