ios - 相机处于横向模式

标签 ios camera avfoundation landscape-portrait

我正在使用 AVFoundation 框架来管理相机。当我处于横向模式时,我希望将拍摄按钮保持在屏幕右侧,如下图所示:

enter image description here

知道怎么做吗?

最佳答案

这是我的解决方案。

1) 禁用自动旋转,(我假设你不想旋转整个 View )

2) 将您的 View Controller 注册为 UIDeviceOrientationDidChangeNotification 的观察者

- (void)viewDidLoad {
    ...
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    ...
}

并确保在释放观察者对象之前调用 removeObserver。

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

3) 处理方向变化

- (void)deviceOrientationDidChange {
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation) {
        case UIDeviceOrientationPortrait:
        {
            [self rotateInterfaceWithDegrees:0.0];
        }
            break;
        case UIDeviceOrientationPortraitUpsideDown:
        {
            [self rotateInterfaceWithDegrees:180.0];
        }
            break;
        case UIDeviceOrientationLandscapeLeft:
        {
            [self rotateInterfaceWithDegrees:90.0];
        }
            break;
        case UIDeviceOrientationLandscapeRight:
        {
            [self rotateInterfaceWithDegrees:270.0];
        }
            break;

        default:
            break;
    }
}

4) 进行旋转变换并应用到你的按钮上

- (void)rotateInterfaceWithDegrees:(NSUInteger)degrees {
    CGAffineTransform transform = CGAffineTransformMakeRotation(degrees*M_PI/180.0);
    [UIView animateWithDuration:0.3   // with animation
                     animations:^{    // optional
                         yourFirstButton.transform = transform;
                         yourSecondButton.transform = transform;
                         ...
                     }];
}

希望对您有所帮助。

关于ios - 相机处于横向模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626934/

相关文章:

ios - 以编程方式禁用单击 UIButton 时的突出显示

linux - 如何显示覆盖视频源的图像?

avfoundation - 如何从具有正确方向的 AVCapturePhoto 生成 UIImage?

ios - UIButton 对 Swift 中的第一次按下没有反应

ios - 水平条形图不从 0 开始

c++ - 创建中间件以使相机兼容 ONVIF

android - 可以使用来自 Camera Remote API 的 BULB 模式

objective-c - 我可以在同一个 session 中使用 AVCaptureVideoDataOutput 和 AVCaptureMovieFileOutput 吗?

ios - AVCaptureSession 输出样本缓冲区保存到 CoreData

ios - 在 iPad 上更改 iOS 应用程序的 UserAgent