在使用Objective C继续录制的同时,从后向前切换相机

时间:2017-08-21 14:03:43

标签: ios objective-c camera

我创建了一个iOS应用程序,我可以将视频和照片保存到我的iPhone上的库中。我的问题是我想在保存前拍摄视频时继续录制。这是最近添加到WhatsApp或其他应用程序的选项,但我无法弄清楚如何做到这一点。

-(BOOL)toggleCamera
{
    BOOL success=NO;

    if (self.cameraCount>1)
    {
        NSError* error;
        AVCaptureDeviceInput* newVedioInput;
        AVCaptureDevicePosition position=[self.videoInput.device position];

        if (position==AVCaptureDevicePositionBack)
        {
            newVedioInput=[[AVCaptureDeviceInput alloc] initWithDevice:self.frontFacingCamera error:&error];
        }
        else if (position==AVCaptureDevicePositionFront)
        {
            newVedioInput=[[AVCaptureDeviceInput alloc] initWithDevice:self.backFacingCamera error:&error];
        }
        else
        {
            return NO;
        }

        if (newVedioInput!=nil)
        {
            [self.session beginConfiguration];
            [self.session removeInput:self.videoInput];
            if ([self.session canAddInput:newVedioInput])
            {
                [self.session addInput:newVedioInput];
                self.videoInput=newVedioInput;
            }
            else
            {
                [self.session addInput:self.videoInput];
            }
            [self.session commitConfiguration];
            success=YES;
        }
        else if (error)
        {
            if ([self.delegate respondsToSelector:@selector(recordManager:didFailWithError:)])
            {
                [self.delegate recordManager:self didFailWithError:error];
            }
        }
    }

    return success;
}

当我这样做时,会话改变并且在翻转相机之前保存视频,这不是我需要的。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您不需要更改输出文件。

您需要通过将配置代码包含在 AVCaptureDeviceInputAVCaptureSession 方法中来重新配置 beginConfiguration()commitConfiguration() 输入。

<块引用>

使用它的同步将以下工作分派到会话的私有队列 方法。例如:sessionQueue.sync{}

self.session.beginConfiguration()
if let videoInput = self.videoInput {
    self.session.removeInput(videoInput)
}
addVideoInput() //adding `AVCaptureDeviceInput` again
configureSessionQuality() //Configuring session preset here.

// Fix initial frame having incorrect orientation
let connection = self.videoOutput?.connection(with: .video)
if connection?.isVideoOrientationSupported == true {
    connection?.videoOrientation = self.orientation
}

//Fix preview mirroring
if self.cameraLocation == .front {
    if connection?.isVideoMirroringSupported == true {
         connection?.isVideoMirrored = true
    }
}

self.session.commitConfiguration()

//Change zoom scale if needed.
do {
    try self.captureDevice?.lockForConfiguration()
    self.captureDevice?.videoZoomFactor = zoomScale
    self.captureDevice?.unlockForConfiguration()
} catch {
    print(error)
}
<块引用>

我在我的生产代码中使用了上述方法。这是工作 完全没问题