文档

iOS推流横屏方案建议

更新时间:

横屏模式下,如果有需求的话,需要对UI做一套横屏的适配,在此套适配方案中,不需要对预览view做旋转。

例如:如果预览画面是全屏,横屏模式下,则不需要对预览画面的view做任何更改,只需要对页面上其他的如点赞button做一下frame的变化。

操作步骤

  1. 检测设备方向。

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDeviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
  2. 设备方向回调中设置横屏推流还是竖屏推流。

    - (void)handleDeviceOrientationDidChange: (UIInterfaceOrientation)interfaceOrientation {
    
     UIDevice *device = [UIDevice currentDevice] ;
     switch (device.orientation) {
     case UIDeviceOrientationFaceUp:
     NSLog(@"屏幕朝上平躺");
     break;
    
     case UIDeviceOrientationFaceDown:
     NSLog(@"屏幕朝下平躺");
     break;
    
     case UIDeviceOrientationUnknown:
     NSLog(@"未知方向");
     break;
    
     case UIDeviceOrientationLandscapeLeft: {
     NSLog(@"屏幕向左横置");
     // 横屏推流
     [self destroySession]; // 销毁推流
     // 建议加一个loading 因为销毁推流在重新推流会关闭在重新开启摄像头采集
     _isScreenHorizontal = YES; // 全局变量 横屏置为YES
     [self testPushCapture]; // 重新推流
     }
     break;
    
     case UIDeviceOrientationLandscapeRight:
     NSLog(@"屏幕向右橫置");
     break;
    
     case UIDeviceOrientationPortrait: {
     NSLog(@"屏幕直立");
     // 竖屏推流
     [self destroySession]; // 销毁推流
     _isScreenHorizontal = NO; // 全局变量 横屏设置为NO
     [self testPushCapture]; // 重新推流
     }
     break;
    
     case UIDeviceOrientationPortraitUpsideDown:
     NSLog(@"屏幕直立,上下顛倒");
     break;
    
     default:
     NSLog(@"无法辨识");
     break;
     }
    
    } 
  3. 注销设备。

    - (void)dealloc{
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     [[UIDevice currentDevice]endGeneratingDeviceOrientationNotifications];
    } 
  • 本页导读 (1)
文档反馈