我正在努力实现一个由AVCaptureVideoPreviewLayer组成的视图。视图应该占据整个屏幕,除了顶部的导航栏(视图嵌入在导航控制器中)。
下面的代码显示了我如何设置捕获会话并将前置摄像头添加为输入(如果可用)。
//Set up capture Session
session = [[AVCaptureSession alloc]init];
session.sessionPreset = AVCaptureSessionPresetPhoto;
//Add a camera as input if we have a front-facing camera available
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
NSError *error;
for (AVCaptureDevice *device in devices) {
if ([device position] == AVCaptureDevicePositionBack) {
//Set our input
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
[session addInput:input];
if (!input) {
NSLog(@"No Input");
}
}
}
下面是配置输出的位置,摄像机输出视图作为子视图添加,帧由视图的边界决定。
//Output
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];
output.videoSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
//Preview Layer
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
previewLayer.frame = self.view.bounds;
self.view.translatesAutoresizingMaskIntoConstraints = NO;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:previewLayer];
正如您在iPhone 6上运行时在下面的屏幕截图中看到的那样,预览图层不会占据整个屏幕(摄像机视图右侧的白色空间和导航栏下方的空白区域应该填满通过预览图层:
然而,它确实在下面的iPhone 5屏幕截图(以及iPhone 4,4S和5S)上适当地填满了屏幕。有没有人知道为什么?
答案 0 :(得分:1)
假设您要在viewDidLoad
中创建此视频预览图层,视图控制器的视图尚未布局,因此其frame
和{{1}一旦视图显示在屏幕上,它们不一定是它们。您必须等到视图布局后才能设置预览图层的正确框架,您可以通过实施视图控制器的bounds
方法来执行此操作,如下所示:
viewDidLayoutSubviews
这可以确保只要视图控制器的视图大小发生变化,预览图层的大小也会发生变化。
答案 1 :(得分:0)
您可以使用AVCaptureVideoPreviewLayer
作为UIView
子类的后备层,然后将此视图的实例作为子视图添加到视图控制器的视图中。通过这种方式,您可以使用约束来定位您想要的视频捕获预览,就像使用任何其他视图一样。
以下是您如何做到这一点的示例:
// MyCaptureVideoPreviewView.h
#import <AVFoundation/AVFoundation.h>
@interface MyCaptureVideoPreviewView : UIView
@property (nonatomic, readonly) AVCaptureVideoPreviewLayer *layer;
@property (nonatomic) AVCaptureSession *session;
- (instancetype)initWithFrame:(CGRect)frame session:(AVCaptureSession *)session;
@end
// MyCaptureVideoPreviewView.m
@implementation MyCaptureVideoPreviewView
@dynamic layer;
+ (Class)layerClass {
return [AVCaptureVideoPreviewLayer class];
}
- (AVCaptureSession *)session {
return self.layer.session;
}
- (void)setSession:(AVCaptureSession *)session {
self.layer.session = session;
}
- (instancetype)initWithFrame:(CGRect)frame session:(AVCaptureSession *)session {
self = [super initWithFrame:frame];
if (self) {
self.session = session;
}
return self;
}
@end
然后在视图控制器的实现中的某处:
MyCaptureVideoPreviewView *previewView = [[MyCaptureVideoPreviewView alloc] initWithFrame:self.view.bounds
session:session];
previewView.layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:previewView];
// Add whatever constraints you want. Here we just match the superview's bounds:
NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[previewView]|" options:kNilOptions
metrics:nil views:NSDictionaryOfVariableBindings(previewView)];
NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[previewView]|" options:kNilOptions
metrics:nil views:NSDictionaryOfVariableBindings(previewView)];
[self.view addConstraints:vConstraints];
[self.view addConstraints:hConstraints];