即使成功切换到前置摄像头,iPhone 仍然显示后置摄像头的视图
Posted
技术标签:
【中文标题】即使成功切换到前置摄像头,iPhone 仍然显示后置摄像头的视图【英文标题】:iPhone still shows rear camera's view even after successfully switching to front camera 【发布时间】:2014-02-22 05:28:53 【问题描述】:我正在使用 AV Foundation,并且我设置了一个按钮,当我点击该按钮时,我想在前置摄像头和后置摄像头之间切换。
如果我运行应用程序并点击按钮,控制台会告诉我已成功将设备更改为前置摄像头,但屏幕仍会显示后置摄像头所看到的内容。
我需要更新屏幕,以便它真正显示前置摄像头看到的内容,并且确实捕捉到前置摄像头看到的内容。
这是连接到我的切换按钮的 IBAction 代码:
-(IBAction)toggleCamera
_inputDevice = [self frontCamera];
这是为“frontCamera”提供动力的方法实现:
- (AVCaptureDevice *)frontCamera
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
if ([device position] == AVCaptureDevicePositionFront)
NSLog(@"Device position during frontCamera method: %d", [device position]);
return device;
return nil;
【问题讨论】:
【参考方案1】:您需要做的不仅仅是设置输入设备。您是否设置了 AVCaptureSession?如果是这样,每次更换相机时,您都需要 (1) 重新配置会话,(2) 删除当前输入,(3) 添加新输入并 (4) 提交会话配置:
// Define a flag in your header file to keep track of the current camera input
BOOL isUsingFrontFacingCamera;
-(IBAction)toggleCamera
// determine the mode to switch to
AVCaptureDevicePosition desiredPosition;
if (isUsingFrontFacingCamera)
desiredPosition = AVCaptureDevicePositionBack;
else
desiredPosition = AVCaptureDevicePositionFront;
for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo])
if ([d position] == desiredPosition)
// configure the session
[session beginConfiguration];
// find and clear all the existing inputs
for (AVCaptureInput *oldInput in [session inputs])
[session removeInput:oldInput];
// add the new input to session
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];
[session addInput:input];
// commit the configuration
[session commitConfiguration];
break;
// update the flag to reflect the current mode
isUsingFrontFacingCamera = !isUsingFrontFacingCamera;
答案改编自此post
【讨论】:
以上是关于即使成功切换到前置摄像头,iPhone 仍然显示后置摄像头的视图的主要内容,如果未能解决你的问题,请参考以下文章
将 iphone 前置摄像头广播到 HTML 元素不起作用 iOS 11.2
iPhone 6 摄像头的新技术 Focus Pixels 是啥意思?