- iOS 12.0+
- Xcode 10.0+
The iOS Camera app allows you to capture photos and movies from both the front and rear cameras. Depending on your device, the Camera app also supports the still capture of depth data, portrait effects matte, and Live Photos.
This sample code project, AVCam, shows you how to implement these capture features in your own camera app. It leverages basic functionality of the built-in front and rear iPhone and iPad cameras.
To use AVCam, you need an iOS device running iOS 12 or later. Because Xcode doesn’t have access to the device camera, this sample won’t work in Simulator. AVCam hides buttons for modes that the current device doesn’t support, such as portrait effects matte delivery on an iPhone 7 Plus.
Configure a Capture Session
AVCapture accepts input data from capture devices like the camera and microphone. After receiving the input,
AVCapture marshals that data to appropriate outputs for processing, eventually resulting in a movie file or still photo. After configuring the capture session’s inputs and outputs, you tell it to start—and later stop—capture.
AVCam selects the rear camera by default and configures a camera capture session to stream content to a video preview view.
Preview is a custom
UIView subclass backed by an
AVCapture. AVFoundation doesn’t have a
Preview class, but the sample code creates one to facilitate session management.
The following diagram shows how the session manages input devices and capture output:
Delegate any interaction with the
AVCapture—including its inputs and outputs—to a dedicated serial dispatch queue (
session), so that the interaction doesn’t block the main queue. Perform any configuration involving changes to a session’s topology or disruptions to its running video stream on a separate dispatch queue, since session configuration always blocks execution of other tasks until the queue processes the change. Similarly, the sample code dispatches other tasks—such as resuming an interrupted session, toggling capture modes, switching cameras, and writing media to a file—to the session queue, so that their processing doesn’t block or delay user interaction with the app.
In contrast, the code dispatches tasks that affect the UI (such as updating the preview view) to the main queue, because
AVCapture, a subclass of
CALayer, is the backing layer for the sample’s preview view. You must manipulate
UIView subclasses on the main thread for them to show up in a timely, interactive fashion.
view, AVCam creates a session and assigns it to the preview view:
For more information about configuring image capture sessions, see Setting Up a Capture Session.
Request Authorization for Access to Input Devices
Once you configure the session, it is ready to accept input. Each
AVCapture—whether a camera or a mic—requires the user to authorize access. AVFoundation enumerates the authorization state using
AVAuthorization, which informs the app whether the user has restricted or denied access to a capture device.
For more information about preparing your app’s
Info for custom authorization requests, see Requesting Authorization for Media Capture on iOS.
Switch Between the Rear- and Front-Facing Cameras
change method handles switching between cameras when the user taps a button in the UI. It uses a discovery session, which lists available device types in order of preference, and accepts the first device in its
devices array. For example, the
video in AVCam queries the device on which the app is running for available input devices. Furthermore, if a user’s device has a broken camera, it won’t be available in the
If the discovery session finds a camera in the proper position, it removes the previous input from the capture session and adds the new camera as an input.
Handle Interruptions and Errors
Interruptions such as phone calls, notifications from other apps, and music playback may occur during a capture session. Handle these interruptions by adding observers to listen for
When AVCam receives an interruption notification, it can pause or suspend the session with an option to resume activity when the interruption ends. AVCam registers
session as a handler for receiving notifications, to inform the user when there’s an interruption to the capture session:
The camera view controller observes
AVCapture to receive a notification when an error occurs:
When a runtime error occurs, restart the capture session:
The capture session may also stop if the device sustains system pressure, such as overheating. The camera won’t degrade capture quality or drop frames on its own; if it reaches a critical point, the camera stops working, or the device shuts off. To avoid surprising your users, you may want your app to manually lower the frame rate, turn off depth, or modulate performance based on feedback from
Capture a Photo
Taking a photo happens on the session queue. The process begins by updating the
AVCapture connection to match the video orientation of the video preview layer. This enables the camera to accurately capture what the user sees onscreen:
After aligning the outputs, AVCam proceeds to create
AVCapture to configure capture parameters such as focus, flash, and resolution:
The sample uses a separate object, the
Photo, for the photo capture delegate to isolate each capture life cycle. This clear separation of capture cycles is necessary for Live Photos, where a single capture cycle may involve the capture of several frames.
Each time the user presses the central shutter button, AVCam captures a photo with the previously configured settings by calling
capture method accepts two parameters:
AVCaptureobject that encapsulates the settings your user configures through the app, such as exposure, flash, focus, and torch.
A delegate that conforms to the
AVCaptureprotocol, to respond to subsequent callbacks that the system delivers during photo capture.
Photo Capture Delegate
Once the app calls
capture, the process for starting photography is over. From that point forward, operations on that individual photo capture happens in delegate callbacks.
Track Results Through a Photo Capture Delegate
capture only begins the process of taking a photo. The rest of the process happens in delegate methods that the app implements.
photoarrives first, as soon as you call
Output(_: will Begin Capture For:)
capture. The resolved settings represent the actual settings that the camera will apply for the upcoming photo. AVCam uses this method only for behavior specific to Live Photos. AVCam tries to tell if the photo is a Live Photo by checking its
livesize; if the photo is a Live Photo, AVCam increments a count to track Live Photos in progress:
Photo Movie Dimensions
photoarrives right after the system plays the shutter sound. AVCam uses this opportunity to flash the screen, alerting to the user that the camera captured a photo. The sample code implements this flash by animating the preview view layer’s
Output(_: will Capture Photo For:)
photoarrives when the system finishes processing depth data and a portrait effects matte. AVCam checks for a portrait effects matte and depth metadata at this stage:
Output(_: did Finish Processing Photo: error:)
photois the final callback, marking the end of capture for a single photo. AVCam cleans up its delegate and settings so they don’t remain for subsequent photo captures:
Output(_: did Finish Capture For: error:)
You can apply other visual effects in this delegate method, such as animating a preview thumbnail of the captured photo.
For more information about tracking photo progress through delegate callbacks, see Tracking Photo Capture Progress.
Capture Live Photos
When you enable capture of Live Photos, the camera takes one still image and a short movie around the moment of capture. The app triggers Live Photo capture the same way as still photo capture: through a single call to
capture, where you pass the URL for the Live Photos short video through the
live property. You can enable Live Photos at the
AVCapture level, or you can configure Live Photos at the
AVCapture level on a per-capture basis.
Since Live Photo capture creates a short movie file, AVCam must express where to save the movie file as a URL. Also, because Live Photo captures can overlap, the code must keep track of the number of in-progress Live Photo captures to ensure that the Live Photo label stays visible during these captures. The
photo delegate method in the previous section implements this tracking counter.
photofires when recording of the short movie ends. AVCam dismisses the Live badge here. Because the camera has finished recording the short movie, AVCam executes the Live Photo handler decrementing the completion counter:
Output(_: did Finish Recording Live Photo Movie For Eventual File At: resolved Settings:)
photofires last, indicating that the movie is fully written to disk and is ready for consumption. AVCam uses this opportunity to display any capture errors and redirect the saved file URL to its final output location:
Output(_: did Finish Processing Live Photo To Movie File At: duration: photo Display Time: resolved Settings: error:)
For more information about incorporating Live Photo capture into your app, see Capturing Still and Live Photos.
Capture Depth Data and Portrait Effects Matte
AVCapture, AVCam queries the capture device to see whether its configuration can deliver depth data and a portrait effects matte to still images. If the input device supports either of these modes, and you enable them in the capture settings, the camera attaches depth and portrait effects matte as auxiliary metadata on a per-photo request basis. If the device supports delivery of depth data, portrait effects matte, or Live Photos, the app shows a button, used to toggle the settings for enabling or disabling the feature.
The camera stores depth and portrait effects matte metadata as auxiliary images, discoverable and addressable through the Image I/O API. AVCam accesses this metadata by searching for an auxiliary image of type
For more information about depth data capture, see Capturing Photos with Depth.
Save Photos to the User’s Photo Library
Before you can save an image or movie to the user’s photo library, you must first request access to that library. The process for requesting write authorization mirrors capture device authorization: show an alert with text that you provide in the
AVCam checks for authorization in the
file callback method, which is where the
AVCapture provides media data to save as output.
For more information about requesting access to the user’s photo library, see Requesting Authorization to Access Photos.
Record Movie Files
AVCam supports video capture by querying and adding input devices with the
.video qualifier. The app defaults to the rear dual camera, but, if the device doesn’t have a dual camera, the app defaults to the wide-angle camera.
Instead of passing settings to the system as with still photography, pass an output URL like in Live Photos. The delegate callbacks provide the same URL, so your app doesn’t need to store it in an intermediate variable.
Once the user taps Record to begin capture, AVCam calls
capture triggered delegate callbacks for still capture,
start triggers a series of delegate callbacks for movie recording.
Track the progress of the movie recording through the delegate callback chain. Instead of implementing
AVCapture. Since the movie-recording delegate callbacks require interaction with the capture session, AVCam makes
Camera the delegate instead of creating a separate delegate object.
filefires when the file output starts writing data to a file. AVCam uses this opportunity to change the Record button to a Stop button:
Output(_: did Start Recording To: from:)
filefires last, indicating that the movie is fully written to disk and is ready for consumption. AVCam takes this chance to move the temporarily saved movie from the given URL to the user’s photo library or the app’s documents folder:
Output(_: did Finish Recording To: from: error:)
In the event that AVCam goes into the background—such as when the user accepts an incoming phone call—the app must ask permission from the user to continue recording. AVCam requests time from the system to perform this saving through a background task. This background task ensures that there is enough time to write the file to the photo library, even when AVCam recedes to the background. To conclude background execution, AVCam calls
file after saving the recorded file.
Take Photos While Recording a Movie
Like the iOS Camera app, AVCam can take photos while also capturing a movie. AVCam captures such photos at the same resolution as the video.