我正在尝试使用AVFoundation拍照。当我将obj-c代码转换为快速执行此操作时,我在试图找到videoConnection的部分运行时程序卡住了。有什么线索吗?

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
    }
    }

我成功获取了设备并进行了实时预览,但无法获得stillImageOutput的videoConnection。
[<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已添加
寻找联系
1个
(lldb)

最佳答案

好的,我解决了我的问题。 e头可以帮助别人。所以这里的问题是:

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

该代码就是我将其Objective-C版本快速翻译的方式。我在网上发现了类似的尝试。但是,一切迅速变得如此容易(但并不明显)。因此,这一行解决了这个问题:
if let videoConnection = stillImageOuput.connectionWithMediaType(AVMediaTypeVideo){//take a photo here}

10-08 11:38