Skip to content

pablogsIO/FrontCameraView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FrontCameraView

License Platform

A preview front camera UIView in Swift

Features

  • Preview front camera
  • Move the preview UIView to every corner of the screen.
  • Record video

Configure Your App's Info.plist File

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.

Check for camera authorization

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 ...
            })
        }

Requirements

Swift 4. Should work with Swift 3

Works on portrait mode

Example Project

You have a fully functional demo in Example folder

Usage

Manually

  • 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 ...
            })
        }
    }

Recording video

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()
        }
    }
}

Delegate

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)")
    }

}

Support/Issues

If you have any questions, please don't hesitate to create an issue.

To-Do

Add Cocoapods

License

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.

About

An UIView that shows you the front camera preview

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages