Present Camera using UIKit
The CameraViewController
class is responsible for displaying an interface to interact with the camera. It provides user interface elements among others to enable the flash, toggle the camera and choose a filter.
Configure and create the view controller#
In order to record and access videos in the camera roll the NSCameraUsageDescription
and NSPhotoLibraryUsageDescription
key need to be present in your Info.plist
file.
In addition, for the audio track of recorded videos the NSMicrophoneUsageDescription
key is also required to be present.
Before instantiating the CameraViewController
we can configure some basic properties.
By default, the camera view controller does not show a cancel button, so that it can be embedded into any other view controller. But since it is presented modally in this example, a cancel button should be visible.
We also only want to enable recording video with the camera to be passed to the editor and not photos, so we set allowedRecordingModes
to [.video]
.
Finally we instantiate the view controller and pass the configuration object to the constructor.
For more detail on configuration the camera view refer to this guides sections.
Handle cancellation#
The camera view controller's cancelBlock
will be called when the user taps the cancel button.
You can use the block to dismiss the presenting controller and handle any clean up tasks.
Handle video recording completion#
The completionBlock
will be called when the user selects a video from the camera roll or finishes recording a video.
Inside the block, we dismiss the camera view, create a video object from the URL passed to the block and instantiate a VideoEditViewController
passing in the video.
It is necessary to pass in the PhotoEditModel
of the camera view controller to preserve any filters that might have been selected.
Note, that we have to make the current class the delegate of the VideoEditViewController
in order to handle export and cancellation there.
Present the camera view controller#
We set modalPresentationStyle
to .fullScreen
to present the camera view inside a modal.