ios - Swift:找不到 AVCaptureStillImageOutput 的连接

标签 ios objective-c ios7 swift

我正在尝试使用 AVFoundation 制作照片。当我将 obj-c 代码翻译为 swift 来执行此操作时,我的程序在运行我尝试查找 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 的视频连接。

[<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)

最佳答案

好的,我解决了我的问题。帮助别人的锄头。所以问题就在这里:

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 版本翻译为 swift 的方式。我在网上发现了类似的尝试。然而,很快一切就变得容易多了(但并不明显)。所以这一行解决了这个问题:

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

关于ios - Swift:找不到 AVCaptureStillImageOutput 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27473531/

相关文章:

ios - 将带有图像的 NSAttributedString 保存到 RTF 文件时遇到问题

ios - SpriteNode/Shapes 改变大小并在返回 GameScene 后消失(Swift SpriteKit)

ios - 如何在 Swift 3 的另一个闭包内设置 UIButton 的 isSelected 属性

objective-c - 解析 JSON 字符串和数组时出现 NSJSONSerialization 问题?

objective-c - 如何使用 VideoToolbox 将音频和视频一起编码为 h264 格式?

ios - Testflight 是否不再支持新版本的应用内通知?

ios - 有人知道如何从导航栏中删除不需要的动画吗?截图

ios - 如何在 Swift 中注册我自己的根证书颁发机构?

ios - 修改正在运行的 SKAction 的速度

ios - 如何在 objective-c 中用给定的字符串替换正则表达式的组1