A preview front camera UIView in Swift
- Preview front camera
- Move the preview UIView to every corner of the screen.
- Record video
iOS requires that your app provides static messages to be displayed to the user when the system asks for camera or microphone permission.
Include the NSCameraUsageDescription and NSMicrophoneUsageDescription key in your app’s Info.plist file.
For each key, provide a message that explains to the user why your app needs to capture media, so that the user can feel confident granting permission to your app.
In your viewDidAppear, add the following code for asking authorization to use the camera(if user has revoked authorization)
if let alertCheckAuthorization = capturePreview?.checkCameraAuthorization() {
self.present(alertCheckAuthorization, animated: true, completion: nil)
} else {
capturePreview?.startPreview(completion: { (_) in
// Do something ...
})
}
Swift 4. Should work with Swift 3
Works on portrait mode
You have a fully functional demo in Example folder
- Drag FrontCameraView.swift and CameraManager.swift into your project
- In your ViewController add the following code
var capturePreview: FrontCameraView?
override func viewDidLoad() {
super.viewDidLoad()
capturePreview = FrontCameraView(frame: CGRect(x: self.view.frame.size.width/2,
y: self.view.frame.size.height/2,
width: self.view.frame.size.width/4,
height: self.view.frame.size.height/4))
capturePreview?.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
self.view.addSubview(capturePreview!)
if let alertCheckAuthorization = capturePreview?.checkCameraAuthorization() {
self.present(alertCheckAuthorization, animated: true, completion: nil)
} else {
capturePreview?.startPreview(completion: { (_) in
// Do something ...
})
}
}
I've added a Record Button from here. Simply add this code to viewDidLoad and viewDidAppear
var recordButton: RecordButton?
override func viewDidLoad() {
.
.
.
let recordButtonSide = self.view.bounds.size.height/10
recordButton = RecordButton(frame: CGRect(x: self.view.bounds.width/2-recordButtonSide/2,
y: self.view.bounds.height/2-recordButtonSide/2,
width: recordButtonSide,
height: recordButtonSide))
recordButton?.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
.
.
.
self.view.addSubview(recordButton!)
}
and conform to RecordButtonDelegate protocol.
extension ViewController: RecordButtonDelegate {
func tapButton(isRecording: Bool) {
if isRecording {
capturePreview?.startRecording()
} else {
capturePreview?.stopRecording()
}
}
}
The ViewController should conform FrontCameraViewDelegate protocol to know when the video has been recorded.
extension ViewController: FrontCameraDelegate {
func videoRecorded(atURL: URL?) {
guard let url = atURL else { return }
print("Video has been recorded at: \(url)")
}
}
If you have any questions, please don't hesitate to create an issue.
Add Cocoapods
FrontCameraView is available under the MIT license. See the LICENSE file for more info.
If you use it, I'll be happy to know about it.