相机视图未显示

时间:2015-07-09 01:47:10

标签: ios iphone xcode swift camera

我试图让相机视图显示相机应用。在弄乱了设置摄像机视图代码后没有成功。我认为这与我的GLKView设置代码有关。故事板中的UIView显示在应用程序上,但其空白(白色)。我没有收到任何错误。

我至少在iOS 7.0上构建了它。

我添加了createGLKView和setupCameraView块只是加入!

编辑:我忘了为UIView设置一个出口。它现在显示,但相机视图是黑色的。我的背景是白色的,所以我知道相机视图显示为黑色。

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()

}

1 个答案:

答案 0 :(得分:2)

假设代码正确,您可以尝试将子视图放在前面。我发现,有时候在添加新的子视图时,它并不总是显示在其他子视图之上并隐藏在它们后面。

self.bringSubviewToFront(view)

编辑:其他人可能会知道您编写的iOS版本。