ios - AVCaptureVideoPreviewLayer displays correctly except on iPhone6 -


i'm working on implementing view consists of avcapturevideopreviewlayer. view supposed take entirety of screen, except navigation bar @ top (the view embedded in navigation controller).

my code below shows how i've set capture session , added front camera input if it's available.

//set capture session session = [[avcapturesession alloc]init]; session.sessionpreset = avcapturesessionpresetphoto;  //add camera input if have front-facing camera available nsarray *devices = [avcapturedevice deviceswithmediatype:avmediatypevideo]; nserror *error;  (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 output configured, , camera output view added subview, frame determined 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 can see in screenshot below when running on iphone 6, preview layer not take entirety of screen (the white space right of camera view , under navigation bar should filled preview layer):

enter image description here

however, fill screen appropriately on iphone 5 screenshot below (as iphone 4, 4s, , 5s). have insight why?

enter image description here

assuming you're creating video preview layer in viewdidload, view controller's view has not been laid out yet, , such frame , bounds not once view shown on screen. you'll have wait until view laid out set correct frame of preview layer, can implementing viewdidlayoutsubviews method of view controller, so:

- (void)viewdidlayoutsubviews {     [super viewdidlayoutsubviews];     self.previewlayer.frame = self.view.bounds; } 

this ensures time view controller's view changes in size, preview layer changes in size.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -