Capture Live Photos like those created in the system Camera app and save them to the Photos library.
A Live Photo is a picture that includes motion and sound from the moments just before and after its capture. Your app can easily capture and record Live Photos using the AVFoundation capture system and the
Enable Live Photo Capture
For a still photo your capture session needs only a video input, but a Live Photo includes sound, so you'll need to also connect an audio capture device to your session:
Because you're already using a built-in camera device for video (see Setting Up a Capture Session), you can simply use the default audio capture device—the system automatically uses the best microphone configuration for the camera position.
Capturing Live Photos requires an internal reconfiguration of the capture pipeline, which takes time and interrupts any in-progress captures. Before shooting your first Live Photo, make sure the pipeline is configured appropriately by enabling Live Photo capture on your
Capture a Live Photo
Once your photo output is ready for Live Photos, you can choose still image or Live Photo capture for each shot. To capture a Live Photo, create an
AVCapture object, choosing the format for the still image portion of the Live Photo and providing a URL for writing the movie portion of the Live Photo. Then, call
capture to trigger capture:
Handle Live Photo Results
A Live Photo appears to users in the Photos app as a single asset, but it's actually composed of separate files: the primary still image, and a movie file containing motion and sound from the moments before and after. The capture system delivers these results separately, as soon as each becomes available.
photo method delivers the still image portion of the Live Photo as an
AVCapture object. Because you'll need to save the still image and movie files together, it's best to extract the image file data from the
AVCapture and keep it until the movie file finishes recording, as shown below. (You can also use this method to indicate in your UI that the still image has been captured.)
photo method fires later, indicating that the URL you specified when triggering the capture now contains a complete movie file. Once you have both the still image and movie portions of your Live Photo, you can save them together:
Save a Live Photo to the Photos Library
PHAsset class to create a single Photos asset consisting of media from multiple files—in the case of a Live Photo, the still image and its paired video. As in Saving Captured Photos, you'll need to wrap that request in a
PHPhoto change block, and first make sure that your app has the user's permission to access Photos.
Track Live Photo Progress
Capturing Live Photos adds two additional steps to the process shown in Tracking Photo Capture Progress: after delivery of the still photo result (step 4), the photo output notifies you of movie capture status (step 5) and delivers the movie result (step 6). (Final cleanup becomes step 7.)
When the user captures a Live Photo in the system Camera app, a "Live" indicator appears for a few seconds to let the user know that video and audio are still being recorded. To provide a similar interface in your app, implement these methods in your photo capture delegate:
photomethod tells you that capture has started: implement this method to show a recording indicator.
Output(_: will Begin Capture For:)
photomethod tells you that a Live Photo movie is no longer recording: implement this method to hide the indicator. (Note that the movie file is not yet available at this time.)
Output(_: did Finish Recording Live Photo Movie For Eventual File At: resolved Settings:)
You can have multiple Live Photo captures running at the same time, so it's best to use these methods to keep track of the number of captures "in flight" and hide your indicator only when that number reaches zero: