alexsalo
alexsalo

Reputation: 1495

Swift: failure to find connection for AVCaptureStillImageOutput

I'm trying to make a photo with AVFoundation. When I translate obj-c code to swift to do that my program get stuck when running part where I try to find a videoConnection. Any clues why?

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

I successfully get devices and live preview but I'm unable to get the videoConnection for 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 Added looking for connections 1 (lldb)

Upvotes: 2

Views: 864

Answers (1)

alexsalo
alexsalo

Reputation: 1495

Ok, I solved my issue. Hoe that helps someone else. So the problem in here:

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

That code is how I translated its version of objective-c to swift. I found the similar attempts online. However, in swift everything is so much easier (yet not obvious). So this one line solves the issue:

if let videoConnection = stillImageOuput.connectionWithMediaType(AVMediaTypeVideo){//take a photo here}

Upvotes: 2

Related Questions