Press "Enter" to skip to content

Implementing Camera with AVFoundation

Howdy!

Today I am going to write about how to implement custom Camera using AVFoundation.

Here I have created this storyboard consisting of two View Controllers. One is Main View Controller and attached to Navigation Controller and the other one is Camera View Controller in which I have a UIView (in gray color), a Button which will help us capture the image/video, an Activity Indicator which will show processing, and a Bar Button for switching camera.
Main Storyboard

In my assets I have.

assets.xcassets

Now lets open your Main View Controller and create IBActions for both of your buttons and set the identifier of its segue to Camera View Controller as ‘capture’ (you can give it any identifier  you like). Also create a variable of type String called keyForCamera.
main
Open your Camera View Controller now and create IBActions and IBOutlets, two functions for initiating Camera for Image and Video respectively and a variable named keyFromMenu of type String.

camera
The keyFromMenu will receive the string from Main View Controller which will tell Camera View Controller about the Camera that’s going to be initiated and set the title on the Navigation Bar.

Create @IBOutlet for the UIView , Activity Indicator and @IBAction for the Capture Button and Switch Camera Bar Button.

capture_outlet

Now come back to your Main View Controller and in your @IBActions paste the following code and override prepareForSegue.

Just to check if our logic works build and run it, it should show you different titles on both of the buttons you created.

After running in the simulator.
After running in the simulator.

Now come back to your Camera View Controller class and declare the following variables in it.

Your initiatePictureCamera function will now look like this.

And your initiateVideoCamera will be something like

In your Capture button action

This will show you an alert when image is saved in the Photos.

Conform your class  Camera View Controller to protocol AVCaptureFileOutputRecordingDelegate. It will provide the recorded video url which can be later saved into Photos, and paste the following code in your class.

Finally for saving Video in Photos.

Don’t forget to add the these frameworks in your Camera View Controller.

At last, run at test it.
Check out this demo.


Here is the full Source Code to this project.

Good day!

Author: Aaqib Hussain

Aaqib is an enthusiastic programmer with the love of Swift and anything that looks like Swift i.e Kotlin. He loves writing code in Swift, and exploring new technology and platforms. He likes to listen to old music. When he is not writing code, he’s probably spend his time watching movies, tv-shows or anime, or either doing some research for writing the next article. He started Kode Snippets in 2015.

4 Comments

  1. Wagenr Hélio
    Wagenr Hélio April 3, 2018

    Good evening, Aaqib Hussain!
    I’m looking for documentation that helps me create an app that allows me to record videos and save them in the gallery (like Snapchat or Instagram Histories).
    Same as the one you performed.
    I’ve reviewed the documentation available on the development apple, but it got a bit confusing for me.
    Could you help, give a look at this poor “lammer” who speaks to you?

    The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which uses Swift. This setting can be set in the build settings editor.

    • Aaqib Hussain
      Aaqib Hussain April 3, 2018

      Hi, I think you’re using two different versions of Swift in your project. By any case one of your installed library uses a different version of Swift than your current project. Make sure they both are the same.
      Hope this helps.
      Cheers.

      • Wagenr Hélio
        Wagenr Hélio April 6, 2018

        Aaqib Hussain, thank you very much for the answer, it helped a bit, but as a young enthusiast I do not understand much of the language and I am still learning from what I find on the internet.
        I would like to create something like the Histories of Instagram or SnapChat.
        I could not replicate this code, I believe, for lack of knowledge and practice with language.
        Is there somewhere I can learn more about the audio and video capturing part?

        • Aaqib Hussain
          Aaqib Hussain April 7, 2018

          Hi Wagenr, Can you explain more about what do you mean by Histories? and I couldn’t really find a tutorial that shows you how to use video and audio together. I’m quite busy now adays, if I got some time I will try to code a sample project.

Leave a Reply

Your email address will not be published. Required fields are marked *