我正在使用以下代码成功录制5秒的视频。但是我需要在录制视频时捕获图像。首先我想知道是否可以在录制时捕获图像。如果可能,如何在iOS swift3中执行此任务。我的实际任务是,当用户单击按钮时,我需要每5秒捕获并保存一个视频和一个图像。有人可以引导我执行此任务吗?
func VideoCaptureMethod() {
//self.cameraView = self.view
self.cameraManager.resumeCaptureSession()
let devices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in devices {
if (device as AnyObject).position == AVCaptureDevice.Position.back{
do{
let input = try AVCaptureDeviceInput(device: device )
if captureSession.canAddInput(input){
captureSession.addInput(input)
sessionOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
if captureSession.canAddOutput(sessionOutput){
captureSession.addOutput(sessionOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraView.layer.addSublayer(previewLayer)
previewLayer.position = CGPoint(x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)
previewLayer.bounds = cameraView.frame
}
captureSession.addOutput(movieOutput)
captureSession.startRunning()
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let fileUrl = paths[0].appendingPathComponent("output.mov")
try? FileManager.default.removeItem(at: fileUrl)
movieOutput.startRecording(to: fileUrl, recordingDelegate: self)
let delayTime = DispatchTime.now() + 5
DispatchQueue.main.asyncAfter(deadline: delayTime) {
print("stopping")
self.movieOutput.stopRecording()
}
}
}
catch{
print("Error")
}
}
}
}