Kashif
Kashif

Reputation: 4632

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?

Upvotes: 1

Views: 468

Answers (1)

Kashif
Kashif

Reputation: 4632

Fixed it by adding a check:

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

Upvotes: 1

Related Questions