我需要在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 层次结构:
最佳答案
尽管这个问题是N年前提出的,但我还是想分享我的确切答案。几分钟后,我就能理解古杰明所说的话。确实,解决方案可以通过编程实现。但是,如果您使用的是XIB或Storyboard,请检查这种层次结构。不要在您的CameraPreviewView(其图层将作为相机的uiview)中放置任何 subview 。
关于ios - 如何在AVCaptureVideoPreviewLayer上显示覆盖,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17965635/