【问题标题】:Swift - AVFoundation on OS X adding sublayer issueSwift - OS X 上的 AVFoundation 添加子层问题
【发布时间】:2015-06-09 22:47:03
【问题描述】:

我正在尝试在自定义视图中预览我的视频设备。 但我得到的只是一个空窗。我发现访问我的相机没有问题。应用程序启动后,我看到我的罗技相机 LED 指示灯亮起。 我假设我的问题是将预览层添加为子层。

这是我的简单代码:

import Cocoa
import AVFoundation

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var video: NSView!
@IBOutlet weak var window: NSWindow!

let captureSession = AVCaptureSession()
var captureDevice : AVCaptureDevice?
var previewLayer : AVCaptureVideoPreviewLayer?

func applicationDidFinishLaunching(aNotification: NSNotification) {

    captureSession.sessionPreset = AVCaptureSessionPresetLow
    let devices = AVCaptureDevice.devices()
    for device in devices {

        if (device.hasMediaType(AVMediaTypeVideo)) {
            captureDevice = device as? AVCaptureDevice
            println(captureDevice)
        }
    }

    if captureDevice != nil {
        beginSession()
    }


}

func beginSession() {

    println("begin")
    var err : NSError? = nil
    captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))

    if err != nil {
        println("error: \(err?.localizedDescription)")
    }



    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer!.frame = self.video.bounds
    previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
    self.video.layer?.addSublayer(previewLayer)
    captureSession.startRunning()
}

func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
}


}

【问题讨论】:

    标签: macos swift avfoundation nsview


    【解决方案1】:

    在 Swift 2 中,错误处理发生了变化。如果您使用 Main.storyboard,则以下方法应该有效:

    class ViewController: NSViewController {
    
    let captureSession = AVCaptureSession()
    var captureDevice: AVCaptureDevice?
    var previewLayer: AVCaptureVideoPreviewLayer?
    var previewPanel: NSView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        captureSession.sessionPreset = AVCaptureSessionPresetLow
        let devices = AVCaptureDevice.devices()
        for device in devices {
            if (device.hasMediaType(AVMediaTypeVideo)) {
                captureDevice = device as? AVCaptureDevice
                //Swift.print("device: \(captureDevice)")
            }
        }
        Swift.print("beginning")
        do {
            let deviceInput = try AVCaptureDeviceInput(device: captureDevice)
            captureSession.addInput(deviceInput)
            // get the CustomView as preview panel
            previewPanel = self.view.subviews.first
            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            previewLayer!.frame = self.view.bounds
            // add layer to preview
            previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
            previewPanel.layer?.addSublayer(previewLayer!)
            captureSession.startRunning()
        } catch let error as NSError {
            Swift.print("Error: no valid camera input in \(error.domain)")
        }
    }
    override var representedObject: AnyObject? {
        didSet {
            // Update the view, if already loaded.
        }
    }
    

    别忘了添加 CustomView。

    【讨论】:

      【解决方案2】:

      解决了。在 Interface Builder 中,我需要在核心动画中添加自定义视图。

      【讨论】:

        【解决方案3】:

        对于我的小型 swift-base AVFoundation 抓取器,这篇问答文章帮助了我。我注意到以下检查对于界面构建器中的“自定义视图”很重要:

        • 属性检查器>勾选“可以同时绘制”:
        • View Effects Inspector > 在“核心动画层列表”中检查您的自定义视图,(甚至可以取消选中主视图控制器)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-11-23
          • 2011-04-26
          • 2010-12-31
          • 2012-08-15
          • 2016-09-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多