Objective c AVCaptureVideoPreviewLayer未显示
目标是简单地显示来自网络摄像头的实时视频输入 这段代码是从苹果的一个例子中提炼出来的,它似乎至少部分工作了,因为相机旁边的led在运行时会亮起 我将ctrl从情节提要中的视图拖动到界面中的outlet属性,它现在作为引用outlet连接,但没有显示任何内容,甚至没有设置黑色背景色 知道我哪里出错了吗 谢谢你的帮助 编辑: 实际上,如果我在Objective c AVCaptureVideoPreviewLayer未显示,objective-c,macos,avfoundation,Objective C,Macos,Avfoundation,目标是简单地显示来自网络摄像头的实时视频输入 这段代码是从苹果的一个例子中提炼出来的,它似乎至少部分工作了,因为相机旁边的led在运行时会亮起 我将ctrl从情节提要中的视图拖动到界面中的outlet属性,它现在作为引用outlet连接,但没有显示任何内容,甚至没有设置黑色背景色 知道我哪里出错了吗 谢谢你的帮助 编辑: 实际上,如果我在[previewViewLayer addSublayer:self.previewLayer]上设置一个断点,它的工作原理与预期一样行并继续在调试器中运行。一
[previewViewLayer addSublayer:self.previewLayer]上设置一个断点,它的工作原理与预期一样代码>行并继续在调试器中运行。一定有时间问题
接口:
@property (retain) AVCaptureDevice *videoDevice;
@property (retain) AVCaptureDeviceInput *videoDeviceInput;
@property (retain) AVCaptureSession *session;
@property (retain) AVCaptureVideoPreviewLayer *previewLayer;
@property (assign) IBOutlet NSView *previewView;
实施:
- (void)viewDidLoad {
[super viewDidLoad];
[self getDevice];
[self initSession];
}
- (void)initSession {
// Create a capture session
// self.session = [[AVCaptureSession alloc] init];
[self setSession:[[AVCaptureSession alloc] init]];
// Attach preview to session
CALayer *previewViewLayer = [self.previewView layer];
[previewViewLayer setBackgroundColor:CGColorGetConstantColor(kCGColorBlack)];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
[self.previewLayer setFrame:[previewViewLayer bounds]];
[self.previewLayer setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable];
[previewViewLayer addSublayer:self.previewLayer];
// Start the session
[self.session startRunning];
// Create a device input for the device and add it to the session
NSError *error = nil;
AVCaptureDeviceInput *newVideoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:self.videoDevice error:&error];
[self.session addInput:newVideoDeviceInput];
self.videoDeviceInput = newVideoDeviceInput;
[self.session commitConfiguration];
}
- (void)getDevice {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
NSLog(@"Device name: %@", [device localizedName]);
self.videoDevice = device;
}
}
已解决:
我移动了[self initSession]代码>从-(void)viewDidLoad
到-(void)viewDidLayout
更新:
后来我在使用-(void)viewDidLayout
时遇到了更多问题,因此最终我转到了-(void)viewDidAppear