2016-07-13 12 views
0

私はglkviewを使用してビデオフィードを実装しようとしますが、回転を回転しようとすると、常にconnectionWithMediaTypeからnilが返されます。ここconnectionWithMediaType return nil

は私が間違って何をした

override public func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 
    videoFeed = GLKView(frame: self.view.bounds, context: EAGLContext(API: .OpenGLES2)) 
    videoFeed.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 
    videoFeed.translatesAutoresizingMaskIntoConstraints = true 
    videoFeed.contentScaleFactor = 1.0 
    self.view.addSubview(videoFeed) 
    renderContext = CIContext(EAGLContext: videoFeed.context) 
    sessionQueue = dispatch_queue_create("dCamSession", DISPATCH_QUEUE_SERIAL) 
    videoFeed.bindDrawable() 
} 

override public func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 
    startSession() 
} 

func createSession() -> AVCaptureSession { 
    let cam = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 
    var input:AVCaptureInput 
    do { 
     input = try AVCaptureDeviceInput(device: cam) 
    } catch _ as NSError { 
     print("Cannot Init Cam") 
     exit(EXIT_FAILURE) 
    } 

    //output 
    let videoOut = AVCaptureVideoDataOutput() 

    videoOut.videoSettings = nil 
    videoOut.alwaysDiscardsLateVideoFrames = true 
    videoOut.setSampleBufferDelegate(self, queue: sessionQueue) 
    //connectionWithMediaType always get nil 
    let connection = videoOut.connectionWithMediaType(AVMediaTypeVideo) 
    if connection.supportsVideoOrientation { 
     connection.videoOrientation = .Portrait 
    } 

    let session = AVCaptureSession() 
    //make sure the stream quality good enough. 
    session.sessionPreset = AVCaptureSessionPresetPhoto 
    session.addInput(input) 
    session.addOutput(videoOut)   
    session.commitConfiguration() 

    return session 

} 

func startSession() { 
    if camSession == nil { 
     camSession = createSession() 
    } 
    camSession.startRunning() 
} 

私のセットアップですか?

私はvideoOrientationをすべて正常よりも削除していますが、方向は間違っています。

あなたが必要とするのは、非同期に呼び出すことだけです。

答えて

4

あなたの問題は、出力としてvideoOutを追加する前に接続にアクセスしようとしていたことです。

これは、非同期で動作する理由です。connectionWithMediaTypeに電話するときには、addOutput(videoOut)が既に呼び出されています。

+1

ありがとうbro !!!!!! –

関連する問題