iphone - 如何像 iPhone 上的标准相机应用程序一样处理方向的变化

标签 iphone ios camera-overlay

我使用 AVFoundation 框架来访问相机。 在标准相机应用程序中,当方向发生变化时,只会旋转按钮上的图标。 但是当我尝试做类似的事情时,我遇到了与方向变化动画相关的问题。预览 View 被旋转,它后面的区域是黑色的,并且在旋转时有图像抖动。

现在我有了这段代码:

-(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];
}

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

如何创建相机预览 View 并解决方向变化问题? 可能我必须使用其他仪器才能使用相机吗?不是 AVFoundation 框架?

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

抱歉英语不好。

最佳答案

您需要为您的 Controller 禁用横向并通过处理方向更改通知更新图标布局等。在本主题中描述正确 - How to handle autorotation in AVCaptureVideoPreviewLayer?

关于iphone - 如何像 iPhone 上的标准相机应用程序一样处理方向的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14448192/

相关文章:

ios - 导航栏中的 UINavigationItem 位置在方向上没有改变

ios - 带有用户照片的相机叠加层未保存为已编辑

ios - 在 UIImagePickerController 中启用相机胶卷缩略图

iphone - 如何在 Objective-C 中编写计时器?

iphone - UIToolbar - 边距?有谁知道 UIToolbar 的左右边距吗?

ios - Assets 显示错误时间

ios - ARC ViewController 被释放

iphone - 我是否总是必须在 -viewDidLoad 方法中调用 [super viewDidLoad] ?

iphone - 在 iPhone 应用程序中发送/存储凭据

ios - UIImagePicker cameraOverlayView 出现在重拍屏幕上