Skip to main content
PESDK/iOS/Guides/Camera
Language:

Configure Camera

PhotoEditor SDK supports several configuration options for the CameraViewController allowing flexible adaptation to different needs and use cases. For a detailed explanation of how to configure different editor views, refer to this guide.

showCancelButton#

By default, the camera view controller does not show a cancel button, so that it can be embedded into any other view controller. If presented inside a modal, however, as in this example we can configure the button to be visible.

allowedRecordingModes#

By default, the camera view controller allows to both record a photo as well as take a photo. Since, in this example, we are only using VE.SDK, we disable taking photos.

allowedCameraPositions#

The default configuration allows all camera positions. If, however, we want to open with the front camera initially. for example in a social media application, we can configure the order of allowed camera positions.

The camera does not crop the image to a square by default. In this example, we force the camera to crop the input image to a square, in order to have symmetric image dimensions for e.g. a profile picture.

allowedRecordingOrientations#

There are also no default constraints on recording modes. To specify a set of allowed recording orientations assign them as an array. If the current orientation of the device does not match any of the specified orientations the first one of these is used.

File:
import PhotoEditorSDK
import UIKit
class PhotoCameraConfigurationSwift: Example {
override func invokeExample() {
// Create a `Configuration` object.
//
// For replacing the live filters of the camera please see the
// examples at Filters/AddFilters.
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
// By default the camera view controller allows to both record a video as well as
// take a photo. Since we are only using PE.SDK here, recording a video should not
// be allowed.
options.allowedRecordingModes = [.photo]
// By default the camera view controller allows all camera positions. The first
// position will be selected by default.
// For this example, the camera should open with the front camera first, e.g.
// for taking a profile picture.
options.allowedCameraPositions = [.front, .back]
// By default the camera does not crop the image to a square.
// In this example, we force the camera to crop the input image to a
// square, e.g. for having symmetric profile picture image dimensions.
options.cropToSquare = true
// By default, the camera allows all recording modes. If the current orientation
// of the device does not match any of the specified orientations the first one
// of these is used.
// For this example, we only allow the `.portrait` orientation, e.g. for all
// profile images to have the same orientation.
options.allowedRecordingOrientations = [.portrait]
}
}
// Create the camera view controller passing above configuration.
// The camera only runs on physical devices. On the simulator, only the image picker is enabled.
let cameraViewController = CameraViewController(configuration: configuration)
cameraViewController.modalPresentationStyle = .fullScreen
// The `completionBlock` will be called once a photo has been taken or when the user selects a photo from the camera roll.
cameraViewController.completionBlock = { result in
// You can now use `result.data` for further processing.
// The `result.data` will contain the photo in JPEG format with all EXIF information.
// The `result.url` is not needed since we are only using PE.SDK.
// Dismissing the camera now.
guard let data = result.data else { return }
print("Received image with \(data.count) bytes")
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
// The `cancelBlock` will be called when the user taps the cancel button.
cameraViewController.cancelBlock = {
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
presentingViewController?.present(cameraViewController, animated: true, completion: nil)
}
}