fix(ios): 修复摄像头方向在 iOS 13+ 上的兼容性问题
添加辅助方法获取当前界面方向并转换为摄像头方向,兼容 iOS 13+ 的新 API
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.history/
|
||||||
@@ -784,21 +784,58 @@ prompt:(NSString *)prompt {
|
|||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
- (void)viewDidAppear:(BOOL)animated {
|
- (void)viewDidAppear:(BOOL)animated {
|
||||||
|
[super viewDidAppear:animated]; // 建议先调用 super,符合规范
|
||||||
|
|
||||||
// setup capture preview layer
|
// setup capture preview layer
|
||||||
AVCaptureVideoPreviewLayer* previewLayer = self.processor.previewLayer;
|
AVCaptureVideoPreviewLayer* previewLayer = self.processor.previewLayer;
|
||||||
previewLayer.frame = self.view.bounds;
|
previewLayer.frame = self.view.bounds;
|
||||||
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
|
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
|
||||||
|
|
||||||
if ([previewLayer.connection isVideoOrientationSupported]) {
|
if ([previewLayer.connection isVideoOrientationSupported]) {
|
||||||
[previewLayer.connection setVideoOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
|
// 1. 获取当前界面方向(兼容 iOS 13+)
|
||||||
|
UIInterfaceOrientation interfaceOrientation = [self currentInterfaceOrientation];
|
||||||
|
// 2. 转换为相机需要的方向类型
|
||||||
|
AVCaptureVideoOrientation captureOrientation = [self convertToCaptureOrientation:interfaceOrientation];
|
||||||
|
// 3. 设置方向
|
||||||
|
[previewLayer.connection setVideoOrientation:captureOrientation];
|
||||||
}
|
}
|
||||||
|
|
||||||
[self.view.layer insertSublayer:previewLayer below:[[self.view.layer sublayers] objectAtIndex:0]];
|
[self.view.layer insertSublayer:previewLayer below:[[self.view.layer sublayers] objectAtIndex:0]];
|
||||||
|
|
||||||
[self.view addSubview:[self buildOverlayView]];
|
[self.view addSubview:[self buildOverlayView]];
|
||||||
[self startCapturing];
|
[self startCapturing];
|
||||||
|
}
|
||||||
|
|
||||||
[super viewDidAppear:animated];
|
#pragma mark - 辅助方法:获取当前界面方向(兼容 iOS 13+)
|
||||||
|
- (UIInterfaceOrientation)currentInterfaceOrientation {
|
||||||
|
if (@available(iOS 13.0, *)) {
|
||||||
|
// iOS 13+ 用 windowScene 获取方向(statusBarOrientation 已废弃)
|
||||||
|
UIWindowScene *windowScene = [UIApplication sharedApplication].windows.firstObject.windowScene;
|
||||||
|
return windowScene.interfaceOrientation;
|
||||||
|
} else {
|
||||||
|
// iOS 12 及以下用 statusBarOrientation
|
||||||
|
return [UIApplication sharedApplication].statusBarOrientation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - 辅助方法:将 UIInterfaceOrientation 转为 AVCaptureVideoOrientation
|
||||||
|
- (AVCaptureVideoOrientation)convertToCaptureOrientation:(UIInterfaceOrientation)interfaceOrientation {
|
||||||
|
switch (interfaceOrientation) {
|
||||||
|
case UIInterfaceOrientationPortrait:
|
||||||
|
return AVCaptureVideoOrientationPortrait;
|
||||||
|
case UIInterfaceOrientationPortraitUpsideDown:
|
||||||
|
return AVCaptureVideoOrientationPortraitUpsideDown;
|
||||||
|
case UIInterfaceOrientationLandscapeLeft:
|
||||||
|
// 注意:界面左横屏对应相机右横屏(镜像关系)
|
||||||
|
return AVCaptureVideoOrientationLandscapeRight;
|
||||||
|
case UIInterfaceOrientationLandscapeRight:
|
||||||
|
// 注意:界面右横屏对应相机左横屏(镜像关系)
|
||||||
|
return AVCaptureVideoOrientationLandscapeLeft;
|
||||||
|
case UIInterfaceOrientationUnknown:
|
||||||
|
default:
|
||||||
|
// 未知方向默认竖屏
|
||||||
|
return AVCaptureVideoOrientationPortrait;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user