AVCaptureVideoPreviewLayer smooth orientation rotation

后端 未结 9 900
Happy的楠姐
Happy的楠姐 2021-02-02 16:45

I\'m trying to disable any discernable orientation rotation to an AVCaptureVideoPreviewLayer while still maintaining rotation for any subviews. AVCaptureVideoPreviewLayer does h

9条回答
  •  醉梦人生
    2021-02-02 17:14

    I wanted the AVCaptureVideoPreviewLayer to handsomely follow all rotations. This is how I did it (overlayView is just a view that happens to have the correct bounds). It only animates without a hick-up when you place your calls to super exactly where they are in the code:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
        if ([[[self previewLayer] connection] isVideoOrientationSupported])
        {
            [[[self previewLayer] connection] setVideoOrientation:toInterfaceOrientation];
        }
    }
    
    - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        CGRect layerRect = [[self overlayView] bounds];
        [[self previewLayer] setBounds:layerRect];
        [[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                                     CGRectGetMidY(layerRect))];
    
        [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration: duration];
    }
    

提交回复
热议问题