Swift - OS X上的AVFoundation添加了子层问题

时间:2015-06-09 22:47:03

标签: macos swift avfoundation nsview

我正在尝试在自定义视图中预览我的视频设备。 但我得到的只是一个空窗口。我看到我访问相机没问题。一旦应用程序启动,我就会看到我的罗技相机开启了。 我认为我的问题是将预览图层添加为子图层。

这是我的简单代码:

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
}


}

3 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

对于我的小型快速AVFoundation抓取器,这个Q& A帖子帮助了我。我注意到以下检查对于"自定义视图"在“界面”构建器中:

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