**You can auto capturing both camera image and video recording by use this code.**
import UIKit
import AVFoundation
import MobileCoreServices
class ViewController: UIViewController, UIGestureRecognizerDelegate {
let captureSession = AVCaptureSession()
var captureDevice : AVCaptureDevice?
var imagePicker = UIImagePickerController()
var flagVideoRecording = false
var arrImages = [UIImage]()
var countVideoRecording = 0
var labelTime = UILabel()
var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(actionRepeatCapturing), name: .AVCaptureSessionDidStartRunning, object: nil)
}
@objc func actionRepeatCapturing() {
flagVideoRecording = false
startCapturingBothImageAndRecordView()
}
//MARK:- UIButton's Action
@IBAction func actionCaptureImage(_ sender: UIButton) {
flagVideoRecording = false
if AVCaptureDevice.authorizationStatus(for: AVMediaType.video) == AVAuthorizationStatus.authorized {
startCapturingBothImageAndRecordView()
} else {
AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (granted: Bool) -> Void in
if granted == true {
self.startCapturingBothImageAndRecordView()
} else {
DispatchQueue.main.async {
self.alertToEncourageAccessInitially("Camera access required for capturing photos!", actionTitle: "Allow Camera")
}
}
})
}
}
@IBAction func actionCaptureVideo(_ sender: UIButton) {
flagVideoRecording = true
if AVCaptureDevice.authorizationStatus(for: AVMediaType.video) == AVAuthorizationStatus.authorized {
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSession.RecordPermission.granted:
self.startCapturingBothImageAndRecordView()
case AVAudioSession.RecordPermission.denied:
self.alertToEncourageAccessInitially("Microphone access required for record your voice!", actionTitle: "Allow Microphone")
case AVAudioSession.RecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
self.startCapturingBothImageAndRecordView()
} else {
self.alertToEncourageAccessInitially("Microphone access required for record your voice!", actionTitle: "Allow Microphone")
}
})
default:
break
}
} else {
AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (granted: Bool) -> Void in
if granted == true {
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSession.RecordPermission.granted:
self.startCapturingBothImageAndRecordView()
case AVAudioSession.RecordPermission.denied:
self.alertToEncourageAccessInitially("Microphone access required for record your voice!", actionTitle: "Allow Microphone")
case AVAudioSession.RecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
self.startCapturingBothImageAndRecordView()
} else {
self.alertToEncourageAccessInitially("Microphone access required for record your voice!", actionTitle: "Allow Microphone")
}
})
default:
break
}
} else {
DispatchQueue.main.async {
self.alertToEncourageAccessInitially("Camera access required for record video", actionTitle: "Allow Camera")
}
}
})
}
}
}
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func startCapturingBothImageAndRecordView() {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera) {
debugPrint("captureVideoPressed and camera available.")
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
if flagVideoRecording {
imagePicker.mediaTypes = [kUTTypeMovie as String]
imagePicker.allowsEditing = false
imagePicker.showsCameraControls = false
let viewTime = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 40))
viewTime.backgroundColor = .black
viewTime.alpha = 0.1
labelTime = UILabel(frame: CGRect(x: self.view.frame.width/2-50, y: 10, width: 100, height: 25))
labelTime.font = UIFont.boldSystemFont(ofSize: 17)
labelTime.text = "00.00:00"
labelTime.textColor = .white
labelTime.textAlignment = .center
labelTime.backgroundColor = .red
imagePicker.view.addSubview(viewTime)
imagePicker.view.addSubview(labelTime)
self.timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(self.actionStopVideoRecording),
userInfo: nil,
repeats: true)
} else {
imagePicker.allowsEditing = false
imagePicker.showsCameraControls = false
}
} else {
debugPrint("Camera not available.")
}
self.present(self.imagePicker, animated: true, completion: {
if self.flagVideoRecording {
self.imagePicker.startVideoCapture()
} else {
self.imagePicker.takePicture()
}
})
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if flagVideoRecording {
if let videoFileURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
debugPrint(videoFileURL)
// let data = try Data(contentsOf: videoFileURL, options: .mappedIfSafe)
// debugPrint(data)
}
self.dismiss(animated: true, completion: nil)
} else {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
arrImages.append(pickedImage)
}
sleep(1)
if arrImages.count >= 5 {
self.dismiss(animated: true, completion: nil)
} else {
NotificationCenter.default.post(name: .AVCaptureSessionDidStartRunning, object: nil, userInfo: nil)
}
}
}
@objc func actionStopVideoRecording() {
countVideoRecording += 1
labelTime.text = countVideoRecording == 10 ? "00:00:\(countVideoRecording)":"00:00:0\(countVideoRecording)"
if countVideoRecording == 10 {
imagePicker.stopVideoCapture()
timer?.invalidate()
timer = nil
}
}
}
extension ViewController {
func alertToEncourageAccessInitially(_ msgString: String, actionTitle: String) {
let alert = UIAlertController(
title: "IMPORTANT",
message: msgString,
preferredStyle: UIAlertController.Style.alert
)
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: actionTitle, style: .destructive, handler: { (alert) -> Void in
let myUrl = URL(string: UIApplication.openSettingsURLString)!
if let url = URL(string: "\(myUrl)"), !url.absoluteString.isEmpty {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
// or outside scope use this
guard let url = URL(string: "\(myUrl)"), !url.absoluteString.isEmpty else {
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}))
present(alert, animated: true, completion: nil)
}
}