iOS CAlayer Orientation AVCaptureVideoPreviewLayer doesn't rotate

后端 未结 8 731
既然无缘
既然无缘 2020-12-24 09:50

\"enter Summary: I can\'t force the CALayer to respond correctly to orientati

相关标签:
8条回答
  • 2020-12-24 10:42
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration  
    {  
        previewLayer.orientation = toInterfaceOrientation;  
    }
    
    0 讨论(0)
  • 2020-12-24 10:44

    I am still not sure what's causing the problem but I managed to fix it. Here is how I did it:

    in viewDidLoad I am adding a layer:

    CGRect layerRect = [[[self view] layer] bounds];
        [[[self captureManager] previewLayer] setBounds:layerRect];
        [[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
        [[[self view] layer] addSublayer:[[self captureManager] previewLayer]];
    

    Then I am adding call to the rotateLayer method to didRotate

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
        [self rotateLayer];
    }
    

    and finally the rotateLayer method looks like:

    -(void)rotateLayer{
        CALayer * stuckview = [[self captureManager] previewLayer];
        CGRect layerRect = [[[self view] layer] bounds];
    
        UIDeviceOrientation orientation =[[UIDevice currentDevice]orientation];
    
        switch (orientation) {
            case UIDeviceOrientationLandscapeLeft:
                stuckview.affineTransform = CGAffineTransformMakeRotation(M_PI+ M_PI_2); // 270 degress
    
                break;
            case UIDeviceOrientationLandscapeRight:
                stuckview.affineTransform = CGAffineTransformMakeRotation(M_PI_2); // 90 degrees
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                stuckview.affineTransform = CGAffineTransformMakeRotation(M_PI); // 180 degrees
                break;
            default:
                stuckview.affineTransform = CGAffineTransformMakeRotation(0.0);
                [stuckview setBounds:layerRect];
                break;
        }
        [stuckview setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
    }
    

    I still don't understand why it works in this way. If anyone can explain it will be great.

    0 讨论(0)
提交回复
热议问题