gpt4 book ai didi

ios - 即使成功切换到前置摄像头,iPhone 仍然显示后置摄像头的 View

转载 作者:行者123 更新时间:2023-11-29 03:13:35 25 4
gpt4 key购买 nike

我正在使用 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;
}

最佳答案

您需要做的不仅仅是设置输入设备。你有设置 AVCaptureSession 吗?如果是这样,每次交换相机时,您都需要 (1) 重新配置 session ,(2) 删除当前输入,(3) 添加新输入,以及 (4) 提交 session 配置:

// 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 的答案

关于ios - 即使成功切换到前置摄像头,iPhone 仍然显示后置摄像头的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21950171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com