我正在关注关于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/