Oscar Deng
Oscar Deng

Reputation: 1

Camera View not showing up

I'm trying to get a camera view for a camera app to show. After messing with the setup camera view code without success. I think it has something to do with my GLKView setup code. The UIView in the storyboard shows up on the app but its blank (white). I don't get any errors.

I'm building this for at least iOS 7.0.

I added both the createGLKView and the setupCameraView blocks just incase!

EDIT: I forgot to set an outlet to the UIView. It shows now, but the camera view is black. My background is white so I know that the camera view is showing up black.

func createGLKView() {

    if (self.context != nil) {

        return

    }

    self.context = EAGLContext(API: EAGLRenderingAPI.OpenGLES2)
    //.init(API:EAGLRenderingAPI.OpenGLES2)
    var view: GLKView = GLKView(frame: self.bounds)
    //.init(frame: bounds.self)

    view.autoresizingMask = UIViewAutoresizing(2) | UIViewAutoresizing(5)

    view.translatesAutoresizingMaskIntoConstraints()
    view.context = self.context
    view.contentScaleFactor = 1.0
    view.drawableDepthFormat = GLKViewDrawableDepthFormat.Format24

    self.insertSubview(view, atIndex: 0)

    glkView = view

    glGenRenderbuffers(1, &renderBuffer)
    glBindRenderbuffer(GLenum(GL_RENDERBUFFER), renderBuffer)
    coreImageContext = CIContext(EAGLContext: self.context)
    EAGLContext.setCurrentContext(self.context)

    println("GLKView setup")

}

func setupCameraView() {

    self.createGLKView()

    var possibleDevices: NSArray = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)

    //var device: AVCaptureDevice = possibleDevices.firstObject as! AVCaptureDevice

    var device: AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    /*
    if device != true {

        println("device returned!")
        return

    }
    */

    let devices = AVCaptureDevice.devices()

    // Loop through all the capture devices on this phone
    for camera in devices {
        // Make sure this particular device supports video
        if (camera.hasMediaType(AVMediaTypeVideo)) {
            // Finally check the position and confirm we've got the back camera
            if(camera.position == AVCaptureDevicePosition.Back) {

                device = camera as! AVCaptureDevice

            }
        }
    }


    imageDetectionConfidence = 0.0

    var session: AVCaptureSession = AVCaptureSession()
    self.captureSession = session
    session.beginConfiguration()
    self.captureDevice = device
    /*
    for camera in possibleDevices {

        if camera.position == AVCaptureDevicePosition.Back {

            device = camera as! AVCaptureDevice
            println("back camera selected!")
            break

        }

    }
    */
    var error: NSError?
    /*
    var input: AVCaptureDeviceInput = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &error) as! AVCaptureDeviceInput
    session.sessionPreset = AVCaptureSessionPresetPhoto
    session.addInput(input)
    */

    if let input: AVCaptureDeviceInput = AVCaptureDeviceInput.deviceInputWithDevice(device, error: &error) as? AVCaptureDeviceInput {

        session.sessionPreset = AVCaptureSessionPresetPhoto
        session.addInput(input)

    }

    var dataOutput: AVCaptureVideoDataOutput = AVCaptureVideoDataOutput()
    dataOutput.alwaysDiscardsLateVideoFrames = true

    //dataOutput.videoSettings = NSDictionary(object: Int(kCVPixelFormatType_32BGRA), forKey: kCVPixelBufferPixelFormatTypeKey as String) as [NSObject: AnyObject]

    dataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA]

    dataOutput.setSampleBufferDelegate(self, queue: dispatch_get_main_queue())

    session.addOutput(dataOutput)

    self.stillImageOutput = AVCaptureStillImageOutput()
    session.addOutput(self.stillImageOutput)

    var connection: AVCaptureConnection = (dataOutput.connections as NSArray).firstObject as! AVCaptureConnection

    connection.videoOrientation = AVCaptureVideoOrientation.Portrait

    if device.flashAvailable {

        device.lockForConfiguration(nil)
        device.flashMode = AVCaptureFlashMode.Off
        device.unlockForConfiguration()

        if device.isFocusModeSupported(AVCaptureFocusMode.ContinuousAutoFocus) {

            device.lockForConfiguration(nil)
            device.focusMode = AVCaptureFocusMode.ContinuousAutoFocus
            device.unlockForConfiguration()

        }

    }

    session.commitConfiguration()

}

Upvotes: 0

Views: 1203

Answers (1)

miniluigi008
miniluigi008

Reputation: 49

Assuming that the code is correct, you could try to bring your subview to the front. I've found that sometimes when adding a new subview it isn't always shown above the others and gets hidden behind them.

self.bringSubviewToFront(view)

EDIT: It might help for others to know what iOS version you are programming for.

Upvotes: 2

Related Questions