gpt4 book ai didi

ios - 使用 AVCaptureSession 切换前后摄像头

转载 作者:技术小花猫 更新时间:2023-10-29 10:33:37 25 4
gpt4 key购买 nike

我正在关注 SO 上的唯一答案 -

Switch cameras with avcapturesession

但是 cameraWithPosition 似乎不起作用。已弃用?

//Get new input
AVCaptureDevice *newCamera = nil;
if(((AVCaptureDeviceInput*)currentCameraInput).device.position == AVCaptureDevicePositionBack)
{
newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
}
else
{
newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
}

最佳答案

您需要做的是重新配置您的AVCaptureSession

这是我正在使用的:

// note that `AVCaptureSession * session`
//
if(session)
{
[session beginConfiguration];

AVCaptureInput *currentCameraInput = [session.inputs objectAtIndex:0];

[session removeInput:currentCameraInput];

AVCaptureDevice *newCamera = nil;

if(((AVCaptureDeviceInput*)currentCameraInput).device.position == AVCaptureDevicePositionBack)
{
newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
}
else
{
newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
}

NSError *err = nil;

AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:newCamera error:&err];

if(!newVideoInput || err)
{
NSLog(@"Error creating capture device input: %@", err.localizedDescription);
}
else
{
[session addInput:newVideoInput];
}

[session commitConfiguration];
}

// make sure you have this method in your class
//
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

for (AVCaptureDevice *device in devices)
{
if ([device position] == position)
return device;
}
return nil;
}

关于ios - 使用 AVCaptureSession 切换前后摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31879819/

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