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.

In my assets I have.

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.

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.

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

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.

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!

Share:

administrator

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

  • Wagenr Hélio, April 3, 2018 @ 2:58 am Reply

    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, April 3, 2018 @ 12:06 pm Reply

      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, April 6, 2018 @ 2:27 am Reply

        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, April 7, 2018 @ 11:28 am Reply

          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 *