【发布时间】:2023-03-04 16:32:02
【问题描述】:
我有一个 iOS 应用,在视图控制器中放置了一个简单的 UIView。我试图在UIView 中显示前置摄像头的摄像头馈送。我不是想拍照或录制视频,我只是想在UIView 中显示实时提要。
我尝试实现AVCaptureVideoPreviewLayer,但是我得到的提要是空白的。似乎什么都没有发生。这是我的代码:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input;
@try {
input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
} @catch (NSException *exception) {
NSLog(@"Error; %@", error);
} @finally {
if (error == nil) {
if ([session canAddInput:input]) {
[session addInput:input];
AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
[stillImageOutput setOutputSettings:@{AVVideoCodecKey : AVVideoCodecJPEG}];
if ([session canAddOutput:stillImageOutput]) {
[session setSessionPreset:AVCaptureSessionPresetHigh];
[session addOutput:stillImageOutput];
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
[previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
[backgroundStreamView.layer addSublayer:previewLayer];
[session startRunning];
NSLog(@"session running");
} else {
NSLog(@"cannot add output");
}
} else {
NSLog(@"cannot add inout");
}
} else {
NSLog(@"general error: %@", error);
}
}
会话运行良好,但没有显示视频源。我做错了什么?
【问题讨论】:
标签: objective-c uiview avfoundation avcapturesession avcapturedevice