开发者社区> 问答> 正文

iOS推流横屏方案建议



注意:目前SDK的横屏推流需要在推流界面的Controller中将iPhone竖屏锁定(即只允许Portrait一个方向),因为推流SDK是对采集到的视频帧做的旋转,不是对view做的旋转


[backcolor=transparent]横屏模式下,如果有需求的话,需要对UI做一套横屏的适配,在此套适配方案中,不需要对预览view做旋转,举例:如果预览画面是全屏,横屏模式下,则不需要对预览画面的view做任何更改,只需要对页面上其他的如点赞button做一下frame的变化
步骤:

  • 1、检测设备方向
  1. [backcolor=transparent][[[backcolor=transparent]UIDevice[backcolor=transparent] currentDevice[backcolor=transparent]][backcolor=transparent] beginGeneratingDeviceOrientationNotifications[backcolor=transparent]];
  2. [backcolor=transparent][[[backcolor=transparent]NSNotificationCenter[backcolor=transparent] defaultCenter[backcolor=transparent]][backcolor=transparent] addObserver[backcolor=transparent]:[backcolor=transparent]self[backcolor=transparent] selector[backcolor=transparent]:[backcolor=transparent]@selector[backcolor=transparent]([backcolor=transparent]handleDeviceOrientationDidChange[backcolor=transparent]:)[backcolor=transparent] name[backcolor=transparent]:[backcolor=transparent]UIDeviceOrientationDidChangeNotification[backcolor=transparent] [backcolor=transparent]object[backcolor=transparent]:[backcolor=transparent]nil[backcolor=transparent]];
  • 2、设备方向回调中设置横屏推流还是竖屏推流
  1. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]handleDeviceOrientationDidChange[backcolor=transparent]:([backcolor=transparent]UIInterfaceOrientation[backcolor=transparent])[backcolor=transparent]interfaceOrientation [backcolor=transparent]{
  2. [backcolor=transparent]    [backcolor=transparent]UIDevice[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]device [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]UIDevice[backcolor=transparent] currentDevice[backcolor=transparent]][backcolor=transparent] [backcolor=transparent];
  3. [backcolor=transparent]    [backcolor=transparent]switch[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]device[backcolor=transparent].[backcolor=transparent]orientation[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  4. [backcolor=transparent]        [backcolor=transparent]case[backcolor=transparent] [backcolor=transparent]UIDeviceOrientationFaceUp[backcolor=transparent]:
  5. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"屏幕朝上平躺"[backcolor=transparent]);
  6. [backcolor=transparent]            [backcolor=transparent]break[backcolor=transparent];
  7. [backcolor=transparent]        [backcolor=transparent]case[backcolor=transparent] [backcolor=transparent]UIDeviceOrientationFaceDown[backcolor=transparent]:
  8. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"屏幕朝下平躺"[backcolor=transparent]);
  9. [backcolor=transparent]            [backcolor=transparent]break[backcolor=transparent];
  10. [backcolor=transparent]        [backcolor=transparent]case[backcolor=transparent] [backcolor=transparent]UIDeviceOrientationUnknown[backcolor=transparent]:
  11. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"未知方向"[backcolor=transparent]);
  12. [backcolor=transparent]            [backcolor=transparent]break[backcolor=transparent];
  13. [backcolor=transparent]        [backcolor=transparent]case[backcolor=transparent] [backcolor=transparent]UIDeviceOrientationLandscapeLeft[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]{
  14. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"屏幕向左横置"[backcolor=transparent]);
  15. [backcolor=transparent]            [backcolor=transparent]// 横屏推流
  16. [backcolor=transparent]            [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] destroySession[backcolor=transparent]];[backcolor=transparent]     [backcolor=transparent]// 销毁推流
  17. [backcolor=transparent]            [backcolor=transparent]// 建议加一个loading  因为销毁推流在重新推流会关闭在重新开启摄像头采集
  18. [backcolor=transparent]            _isScreenHorizontal [backcolor=transparent]=[backcolor=transparent] YES[backcolor=transparent];[backcolor=transparent]  [backcolor=transparent]// 全局变量  横屏置为YES
  19. [backcolor=transparent]            [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] testPushCapture[backcolor=transparent]];[backcolor=transparent]    [backcolor=transparent]// 重新推流
  20. [backcolor=transparent]        [backcolor=transparent]}
  21. [backcolor=transparent]            [backcolor=transparent]break[backcolor=transparent];
  22. [backcolor=transparent]        [backcolor=transparent]case[backcolor=transparent] [backcolor=transparent]UIDeviceOrientationLandscapeRight[backcolor=transparent]:
  23. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"屏幕向右橫置"[backcolor=transparent]);
  24. [backcolor=transparent]            [backcolor=transparent]break[backcolor=transparent];
  25. [backcolor=transparent]        [backcolor=transparent]case[backcolor=transparent] [backcolor=transparent]UIDeviceOrientationPortrait[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]{
  26. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"屏幕直立"[backcolor=transparent]);
  27. [backcolor=transparent]            [backcolor=transparent]// 竖屏推流
  28. [backcolor=transparent]            [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] destroySession[backcolor=transparent]];[backcolor=transparent]     [backcolor=transparent]// 销毁推流
  29. [backcolor=transparent]            _isScreenHorizontal [backcolor=transparent]=[backcolor=transparent] NO[backcolor=transparent];[backcolor=transparent]  [backcolor=transparent]// 全局变量  横屏设置为NO
  30. [backcolor=transparent]            [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] testPushCapture[backcolor=transparent]];[backcolor=transparent]    [backcolor=transparent]// 重新推流
  31. [backcolor=transparent]        [backcolor=transparent]}
  32. [backcolor=transparent]            [backcolor=transparent]break[backcolor=transparent];
  33. [backcolor=transparent]        [backcolor=transparent]case[backcolor=transparent] [backcolor=transparent]UIDeviceOrientationPortraitUpsideDown[backcolor=transparent]:
  34. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"屏幕直立,上下顛倒"[backcolor=transparent]);
  35. [backcolor=transparent]            [backcolor=transparent]break[backcolor=transparent];
  36. [backcolor=transparent]        [backcolor=transparent]default[backcolor=transparent]:
  37. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"无法辨识"[backcolor=transparent]);
  38. [backcolor=transparent]            [backcolor=transparent]break[backcolor=transparent];
  39. [backcolor=transparent]    [backcolor=transparent]}
  40. [backcolor=transparent]}
  • 3、注销Device
  1. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]dealloc[backcolor=transparent]{
  2. [backcolor=transparent]    [backcolor=transparent][[[backcolor=transparent]NSNotificationCenter[backcolor=transparent] defaultCenter[backcolor=transparent]][backcolor=transparent] removeObserver[backcolor=transparent]:[backcolor=transparent]self[backcolor=transparent]];
  3. [backcolor=transparent]    [backcolor=transparent][[[backcolor=transparent]UIDevice[backcolor=transparent] currentDevice[backcolor=transparent]][backcolor=transparent]endGeneratingDeviceOrientationNotifications[backcolor=transparent]];
  4. [backcolor=transparent]}

展开
收起
EdgePlus 2017-11-03 10:58:12 16784 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载