目标是从网络摄像头简单地显示实时视频输入。
这段代码是从苹果的示例中提炼出来的,至少在一定程度上似乎已经工作了,因为运行时相机旁边的LED灯亮起来了。
我从storyboard中的视图ctrl-dragged到接口中的outlet属性,并且现在已连接为引用输出,但什么也没有显示出来,甚至没有设置黑色背景颜色。
有什么想法吗?我做错了什么吗?
感谢您的帮助。
编辑: 实际上,如果我在
界面:
这段代码是从苹果的示例中提炼出来的,至少在一定程度上似乎已经工作了,因为运行时相机旁边的LED灯亮起来了。
我从storyboard中的视图ctrl-dragged到接口中的outlet属性,并且现在已连接为引用输出,但什么也没有显示出来,甚至没有设置黑色背景颜色。
有什么想法吗?我做错了什么吗?
感谢您的帮助。
编辑: 实际上,如果我在
[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;
}
}