Present Camera using SwiftUI
Since we are using UIKit in our example application we need to use an UIHostingController
to present a SwiftUI View
. The VideoCameraSwiftUIView
defined below wraps the actual Camera
view to coordinate its dismissal with the presenting UIViewController
.
This process is not necessary for a pure SwiftUI application. You would use the Camera
view directly.
Configure and create the view#
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.
The custom VideoCameraSwiftUIView
acts as the ContentView
of a pure SwiftUI application.
First, we create several instance variables; the video
to reference recorded or selected videos, a boolean variable vesdkPresented
to indicate whether the editor view should be presented and a PhotoEditModel
which holds the edit state of the camera view i.e. filters that have been applied.
Next, we create a configuration object setting allowedRecordingModes
to [.video]
, since we only want to enable recording and selecting videos not photos.
By default, the camera view controller does not show a cancel button, so it can be embedded into any other view controller.
But since it is presented modally in this example, a cancel button should be visible.
Refer to the configuration guide for a detailed explanation of how to configure the editor.
Next, we initialize the Camera
with the configuration object and use the fullScreenCover
in combination with the onChange
modifier to edit the video
with the VideoEditor
.
Handling events#
Using the onDidSave
method allows us to register an event handler that is invoked when a user successfully recorded or selected a video.
The result
argument passed to the block is of type CameraResult
and contains a URL that points to the video and a PhotoEditModel
allowing us to pass on the applied filters to the editor.
Likewise, if the user taps the cancel button we dismiss the camera in this example.