2014-12-14 2 views
2

Я пытаюсь сделать фотографию с AVFoundation. Когда я переводил obj-c-код быстро, чтобы сделать, что моя программа застревает при запуске части, где я пытаюсь найти videoConnection. Какие-нибудь подсказки почему?Swift: не удается найти соединение для AVCaptureStillImageOutput

let captureSesion = AVCaptureSession() 
var captureDevice : AVCaptureDevice? 
let stillImageOutput = AVCaptureStillImageOutput() 

captureSesion.sessionPreset = AVCaptureSessionPresetPhoto 
    let devices = AVCaptureDevice.devices() 
    println(devices) 
    for device in devices{ 
     if device.hasMediaType(AVMediaTypeVideo){ 
      if device.position == AVCaptureDevicePosition.Back{ 
       captureDevice = device as? AVCaptureDevice 
      } 
     } 
    } 
    if captureDevice != nil{ 
     var err : NSError? = nil 
     captureSesion.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err)) 

     //preview 
     var previewLayer = AVCaptureVideoPreviewLayer(session: captureSesion) 
     self.view.layer.addSublayer(previewLayer) 
     previewLayer?.frame = self.view.layer.frame 

     var outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG] 
     stillImageOutput.outputSettings = outputSettings 
     captureSesion.addOutput(stillImageOutput)   
     captureSesion.startRunning() 

     //take photo 
     var videoConnection : AVCaptureConnection? 
     //only have one conniption and debug get stuck here!! 
     for connection in self.stillImageOutput.connections{ 
      for port in connection.inputPorts!{ 
       if port.mediaType == AVMediaTypeVideo{ 
        videoConnection = connection as? AVCaptureConnection 
        break //for ports 
       } 
      } 
      if videoConnection != nil{ 
       break //for connections 
      } 
     } 
     //Take a photo and show 
    } 
    } 

я успешно получить устройства и жить предварительный просмотр, но я не могу получить videoConnection для stillImageOutput.

[<AVCaptureFigVideoDevice: 0x14d4c660 [Back Camera][com.apple.avfoundation.avcapturedevice.built-in_video:0]>, <AVCaptureFigVideoDevice: 0x14e904d0 [Front Camera][com.apple.avfoundation.avcapturedevice.built-in_video:1]>, <AVCaptureFigAudioDevice: 0x14e8a6d0 [iPod Microphone][com.apple.avfoundation.avcapturedevice.built-in_audio:0]>] 

stilImageOutput Добавлено ищет соединения (lldb)

ответ

2

Хорошо, я решил мою проблему. Hoe помогает кому-то другому. Таким образом, проблема здесь:

var videoConnection : AVCaptureConnection? 
    //only have one conniption and debug get stuck here!! 
    for connection in self.stillImageOutput.connections{ 
     for port in connection.inputPorts!{ 
      if port.mediaType == AVMediaTypeVideo{ 
       videoConnection = connection as? AVCaptureConnection 
       break //for ports 
      } 
     } 
     if videoConnection != nil{ 
      break //for connections 
     } 
    }//take a photo then 

Этот код - это то, как я перевел свою версию объектива-c в swift. Я нашел подобные попытки в Интернете. Однако в быстром все намного проще (пока не очевидно). Таким образом, эта одна линия решает проблему:

if let videoConnection = stillImageOuput.connectionWithMediaType(AVMediaTypeVideo){//take a photo here} 
Смежные вопросы