Skip to main content
Language:

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.

Next Steps#

File:
import UIKit
import VideoEditorSDK
class ShowVideoCameraUIKitSwift: Example, VideoEditViewControllerDelegate {
override func invokeExample() {
// Create a `Configuration` object.
let configuration = Configuration { builder in
builder.configureCameraViewController { options in
// 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 here, a cancel button should be visible.
options.showCancelButton = true
// Enable video only.
options.allowedRecordingModes = [.video]
}
}
// Create the camera view controller passing above configuration.
let cameraViewController = CameraViewController(configuration: configuration)
// The `cancelBlock` will be called when the user taps the cancel button.
cameraViewController.cancelBlock = { [weak self] in
// Dismiss the camera view controller.
self?.presentingViewController?.dismiss(animated: true, completion: nil)
}
// The `completionBlock` will be called when the user selects a video from the camera roll
// or finishes recording a video.
cameraViewController.completionBlock = { [weak self] result in
// Create a `Video` from the `URL` object.
guard let url = result.url else { return }
let video = Video(url: url)
// Dismiss the camera view controller and open the video editor after dismissal.
self?.presentingViewController?.dismiss(animated: true, completion: {
// Create and present the video editor. Make this class the delegate of it to handle export and cancelation.
// Passing the `result.model` to the editor to preserve selected filters.
let videoEditViewController = VideoEditViewController(videoAsset: video, photoEditModel: result.model)
videoEditViewController.delegate = self
videoEditViewController.modalPresentationStyle = .fullScreen
self?.presentingViewController?.present(videoEditViewController, animated: true, completion: nil)
})
}
// Present the camera view controller.
// To take photos, the camera will require the `NSCameraUsageDescription` key to be present in your Info.plist file.
// To access photos in the camera roll, the camera will require the `NSPhotoLibraryUsageDescription` key to be present in your Info.plist file.
// To record videos, the camera will require the `NSMicrophoneUsageDescription` key to be present in your Info.plist file.
cameraViewController.modalPresentationStyle = .fullScreen
presentingViewController?.present(cameraViewController, animated: true, completion: nil)
}
// MARK: - VideoEditViewControllerDelegate
func videoEditViewControllerShouldStart(_ videoEditViewController: VideoEditViewController, task: VideoEditorTask) -> Bool {
// Implementing this method is optional. You can perform additional validation and interrupt the process by returning `false`.
true
}
func videoEditViewControllerDidFinish(_ videoEditViewController: VideoEditViewController, result: VideoEditorResult) {
// The user exported a new video successfully and the newly generated video is located at `result.output.url`. Dismissing the editor.
presentingViewController?.dismiss(animated: true, completion: nil)
}
func videoEditViewControllerDidFail(_ videoEditViewController: VideoEditViewController, error: VideoEditorError) {
// There was an error generating the video.
print(error.localizedDescription)
// Dismissing the editor.
presentingViewController?.dismiss(animated: true, completion: nil)
}
func videoEditViewControllerDidCancel(_ videoEditViewController: VideoEditViewController) {
// The user tapped on the cancel button within the editor. Dismissing the editor.
presentingViewController?.dismiss(animated: true, completion: nil)
}
}