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):
however, fill screen appropriately on iphone 5 screenshot below (as iphone 4, 4s, , 5s). have insight why?
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
Post a Comment