Using OpenGLES with Swift for photo preview iPhone

554 views Asked by At

I'm using OpenGLES in Swift for a photo preview instead of using a layer because I want to put filters on the output. I'm using this code I found on objc.io objc.io camera capture tutorial

I get this error -

BSXPCMessage received error for message: Connection interrupted

and the preview does not run - blank white screen

Code follows

import UIKit
import AVFoundation
import GLKit
import OpenGLES

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

let captureSession = AVCaptureSession()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var captureDevice : AVCaptureDevice?
    let devices = AVCaptureDevice.devices()


    for device in devices {

        if (device.hasMediaType(AVMediaTypeVideo)) {

            if(device.position == AVCaptureDevicePosition.Front) {
                captureDevice = device as? AVCaptureDevice
            }
        }
    }


    var err : NSError? = nil
    captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))

    if err != nil {
        println("error: \(err?.localizedDescription)")
    }


    captureSession.sessionPreset = AVCaptureSessionPresetPhoto


    captureSession.startRunning()

    let videoOutput : AVCaptureVideoDataOutput = AVCaptureVideoDataOutput()
    videoOutput.setSampleBufferDelegate(self, queue: dispatch_queue_create("sample buffer delegate", DISPATCH_QUEUE_SERIAL))

    if captureSession.canAddOutput(videoOutput) {
        captureSession.addOutput(videoOutput)
    }



}

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {

    let viewFrame : CGRect = self.view.frame

    let glContext = EAGLContext(API: .OpenGLES2)

    let glView = GLKView(frame: viewFrame, context: glContext)


    let ciContext = CIContext(EAGLContext: glContext)
    let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
    let image = CIImage(CVPixelBuffer: pixelBuffer)
    if glContext != EAGLContext.currentContext() {
        EAGLContext.setCurrentContext(glContext)
    }
    glView.bindDrawable()
    ciContext.drawImage(image, inRect:image.extent(), fromRect: image.extent())
    glView.display()
}
0

There are 0 answers