I'm working on implementing a view that consists of an AVCaptureVideoPreviewLayer. The view is supposed to take up the entirety of the screen, except for the navigation bar at the top (the view is embedded in a navigation controller).
My code below shows how I've set up a capture session and added the front camera as input if it's available.
//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");
}
}
}
Below is where the output is configured, and the camera output view is added as a subview, with the frame determined by the view's bounds.
//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];
As you can see in the screenshot below when running on an iPhone 6, the preview layer does not take up the entirety of the screen (the white space to the right of the camera view and under the navigation bar should be filled up by the preview layer):
However, it does fill up the screen appropriately on the iPhone 5 screenshot below (as well as the iPhone 4, 4S, and 5S). Does anyone have any insight into why?
Assuming you're creating this video preview layer in
viewDidLoad
, the view controller's view has not been laid out yet, and as such itsframe
andbounds
are not necessarily what they will be once the view is shown on screen. You'll have to wait until the view is laid out to set the correct frame of your preview layer, which you can do by implementing theviewDidLayoutSubviews
method of your view controller, like so:This ensures that any time your view controller's view changes in size, your preview layer also changes in size.