gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:14:53 25 4
gpt4 key购买 nike

我正在尝试在自定义 View 中预览我的视频设备。但我得到的只是一个空窗口。我发现访问相机没有问题。应用程序启动后,我就会看到我的罗技摄像头 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
}


}

最佳答案

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

关于macos - Swift - OS X 上的 AVFoundation 添加子层问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30744123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com