我需要在 AVCaptureVideoPreviewLayer 上显示图像(帧)叠加层。 我该怎么做?
-(void) viewDidAppear:(BOOL)animated
{
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetPhoto;
[session commitConfiguration];
CALayer *viewLayer = self.vImagePreview.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];
_stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[_stillImageOutput setOutputSettings:outputSettings];
[session addOutput:_stillImageOutput];
[session startRunning];
}
在视频输出层上添加新 View 不起作用。 预览层显示在所有 View 之上。
我的 View 层次结构:
- 主视图
- 叠加 View
- 视频输出 View
最佳答案
虽然这个问题是 N 年前提出的,但我还是想分享我的确切答案。几分钟后,我能够理解古杰明的话。事实上,解决方案可以通过编程方式完成。但如果您在 XIB 或 Storyboard 中进行操作,请检查这种层次结构。不要将任何 subview 放入您的 CameraPreviewView(其图层将成为相机的 uiview)。
关于ios - 如何在 AVCaptureVideoPreviewLayer 上显示覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17965635/