Skip to content

Integrate Mobile Camera

In this example, we will show you how to initialize the CreativeEditor SDK’s mobile camera in your iOS app.

Explore a full code sample on GitHub.

Requirements

The mobile camera requires iOS 16 and Swift 5.10 (Xcode 15.4) or later.

Using Swift Package Manager

If you use Swift Package Manager to build your app and want to integrate the Creative Engine and UI modules using your regular workflows, add the IMGLYUI Swift Package as a dependency to your project.

This package provides multiple library products. Add the default IMGLYUI library to your app target to add all available UI modules included in this package to your app.

To keep your app size minimal, only add the library product that you need, e.g., only add the IMGLYCamera library if you need to import IMGLYCamera in your code. On the General page of your app target’s Xcode project settings the Frameworks, Libraries, and Embedded Content section lists all used library products.

Usage

This example shows the basic usage of the camera.

Import

You can get started right away by importing the camera module into your own code.

swift import IMGLYCamera

In this integration example, the camera is presented as a modal view after tapping a button.

Button("Use the Camera") {
isPresented = true
}

Initialization

The camera is initialized with EngineSettings. You need to provide the license key that you received from IMG.LY. Optionally, you can provide a unique ID tied to your application’s user. This helps us accurately calculate monthly active users (MAU) and it is especially useful when one person uses the app on multiple devices with a sign-in feature, ensuring they’re counted once.

Camera(.init(license: secrets.licenseKey,
userID: "<your unique user id>")) { result in

Result

The Camera’s onDismiss closure returns a Result<CameraResult, CameraError>. If the user has recorded videos, the .success(_) case contains the CameraResult.

switch result {
case let .success(.recording(recordings)):
let urls = recordings.flatMap { $0.videos.map(\.url) }
let recordedVideos = urls
// Do something with the recorded videos
print(recordedVideos)
case .success(.reaction):
print("Reaction case not handled here")
case let .failure(error):
print(error.localizedDescription)
isPresented = false
}

When using UIKit, it needs to be integrated with a UIHostingController object into a UIKit view hierarchy.

private lazy var camera = UIHostingController(rootView:
Camera(.init(license: secrets.licenseKey,
userID: "<your unique user id>")) { result in
switch result {
case let .success(.recording(recordings)):
let urls = recordings.flatMap { $0.videos.map(\.url) }
let recordedVideos = urls
// Do something with the recorded videos
print(recordedVideos)
case .success(.reaction):
print("Reaction case not handled here")
case let .failure(error):
print(error.localizedDescription)
self.presentedViewController?.dismiss(animated: true)
}
})

Environment

When using SwiftUI, the camera is best opened in a fullScreenCover.

.fullScreenCover(isPresented: $isPresented) {

Full Code

Here’s the full code:

import IMGLYCamera
import SwiftUI
struct CameraSwiftUI: View {
@State private var isPresented = false
var body: some View {
Button("Use the Camera") {
isPresented = true
}
.fullScreenCover(isPresented: $isPresented) {
Camera(.init(license: secrets.licenseKey,
userID: "<your unique user id>")) { result in
switch result {
case let .success(.recording(recordings)):
let urls = recordings.flatMap { $0.videos.map(\.url) }
let recordedVideos = urls
// Do something with the recorded videos
print(recordedVideos)
case .success(.reaction):
print("Reaction case not handled here")
case let .failure(error):
print(error.localizedDescription)
isPresented = false
}
}
}
}
}