【发布时间】: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