1

我使用 AVFoundation 框架来访问相机。在标准相机应用程序中,当方向改变时,仅按钮上的图标旋转。但是当我尝试做类似的事情时,我遇到了与方向更改动画相关的问题。预览视图已旋转,其后面的区域为黑色,并且在转动时出现图像抖动。

现在我有这个代码:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:duration]
                     forKey:kCATransactionAnimationDuration];
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft){
        captureManager.previewLayer.transform=CATransform3DMakeRotation(M_PI/2, 0, 0, 1.0);
        captureManager.previewLayer.frame = CGRectMake(0, 0, 480, 320);
    } else if (toInterfaceOrientation==UIInterfaceOrientationPortrait){
        captureManager.previewLayer.transform=CATransform3DMakeRotation(0, 0, 0, 1.0);
        captureManager.previewLayer.frame = CGRectMake(0, 0, 320, 480);
    } else if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight){
        captureManager.previewLayer.transform=CATransform3DMakeRotation(M_PI+M_PI/2, 0, 0, 1.0);
        captureManager.previewLayer.frame = CGRectMake(0, 0, 480, 320);
    }

    [CATransaction commit];
}

我的想法是使用变换动画来处理旋转动画,但我遇到了 glithes 的问题。

如何创建相机预览视图并解决方向更改问题?可能我必须使用其他仪器来使用相机吗?不是 AVFoundation 框架?

我知道我可以锁定方向并使用加速度计来处理按钮图标。但是我遇到了一些子视图控制器的问题。

对不起糟糕的英语。

4

3 回答 3

1

您需要通过处理方向更改通知来禁用控制器的横向方向并更新图标布局等。在本主题中,它的描述是正确的 -如何在 AVCaptureVideoPreviewLayer 中处理自动旋转?

于 2013-02-23T22:25:32.440 回答
0

Nikita 的回答有效,但需要额外的代码来处理设备旋转时 UI 元素的行为。如果这太麻烦了,另一种选择(我实现并工作)是拥有 2 个不同的 UIWindow 实例,一个用于相机的预览层,一个用于显示 UI 元素。请注意,要使其正常工作,带有 UI 元素的 Window 需要具有透明背景。

于 2018-11-20T11:39:26.400 回答
0

我通过以下方式处理预览旋转更改。

PS所有子视图都在情节提要上设置了相应的约束,所以我只更新videoPreviewLayer的帧。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    DispatchQueue.main.async {
        self.videoPreviewLayer.frame = self.previewView.bounds
        self.setRotation()
    }
}



func setRotation() {
    let deviceOrientation = UIDevice.current.orientation
    switch deviceOrientation {
    case .landscapeLeft:
        videoPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.landscapeRight
        break
    case .landscapeRight:
        videoPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.landscapeLeft
        break
    case .portrait:
        videoPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
        break
    case .portraitUpsideDown:
        videoPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portraitUpsideDown
        break
    default:
        break
    }
}
于 2019-11-12T15:05:48.620 回答