Reputation: 324
Can anybody please explain to me why I am getting an error in this piece of code My code never gets to the print statement that says, "Video Preview Layer added as sublayer". I am not sure why this is at all. The video preview layer is obviously created just before hand and then sent to be the sublayer of the camera view. My application is a paged based application. This is my second view that I have. My root view controller is blank for now, I am just trying to make sure the transition to my camera is seamless. Everything works when the camera is the 1st page in my application, or only one view controller. But for some reason now, it is telling me there is an unwrapped nil
/* Start The Capture Session */
func startSession() {
println("Now starting the session")
println("About to add session inputs...")
var error: NSError? = nil
let videoCapture = AVCaptureDeviceInput(device: self.cameraCaptureDevice, error: &error)
if error != nil {
println("Error, failed to add camera Capture Device: \(error?.description)")
}
// add video input
if self.session.canAddInput(videoCapture) {
self.session.addInput(videoCapture)
}
println("Start configuring the capture")
// config capture session
if !session.running {
// set JPEG output
self.stillImageOutput = AVCaptureStillImageOutput()
let outputSettings = [ AVVideoCodecKey : AVVideoCodecJPEG ]
self.stillImageOutput!.outputSettings = outputSettings
println("Successfully configured stillImageOutput")
// add output to session
println("Adding still image output to capture session")
if self.session.canAddOutput(stillImageOutput) {
self.session.addOutput(stillImageOutput)
}
println("Successfully added still image output")
println("Displaying camera in UI")
// display camera in UI
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
println("Video Preview Layer set")
cameraView.layer.addSublayer(videoPreviewLayer)
println("Video Preview Layer Added as sublayer")
videoPreviewLayer!.frame = cameraView.layer.frame
println("Video Preview frame set")
videoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
println("Camera successully can display")
// start camera
self.session.startRunning()
println("Capture Session initiated")
}
}
Upvotes: 0
Views: 207
Reputation: 535944
The problem is that you are running this code at a time when cameraView
is nil. You need to ask yourself why that is. If cameraView
is an @IBOutlet
, then this could happen, for example, if you call startSession
from outside this view controller, at a time when the view controller is being created but has not yet called its own viewDidLoad
(outlet connections are not connected until viewDidLoad
has been called).
Upvotes: 2