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 photos in the camera roll the NSCameraUsageDescription
and NSPhotoLibraryUsageDescription
key need to be present in your Info.plist
file.
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 photos with the camera to be passed to the editor and not videos, so we set allowedRecordingModes
to [.photo]
.
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 photo recording completion#
The completionBlock
will be called when the user selects a photo from the camera roll or finishes recording a photo.
Inside the block, we dismiss the camera view, create a photo object from the data passed to the block and instantiate a PhotoEditViewController
passing in the photo.
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 PhotoEditViewController
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.