Article

Saving Captured Photos

Add the image and other data from a photo capture to the Photos library.

Overview

When you complete a photo capture with AVCapturePhotoOutput, you receive an AVCapturePhoto object that contains not only the still image data, but also camera metadata and any auxiliary images you requested in the capture (like thumbnails or depth maps). You can retrieve these types of data individually from the AVCapturePhoto, or simply call its fileDataRepresentation() method to get a Data object ready for writing to disk, using the codec and file format you requested for that photo in AVCapturePhotoSettings.

Typically, after capturing a photo you'll want to add that data to the user's Photos library; you can do so using the Photos framework.

Request Permission to Use the Photos Library

Just as iOS protects user privacy by requiring user permission to access cameras and microphones, the system also requires the user to grant your app access to the Photos library. To get permission for your app:

  1. Configure your Info.plist file. Include the NSPhotoLibraryUsageDescription key in your app’s Info.plist file. The value for this key is a message to be displayed to the user in the system alert asking for Photos library access. Use this message to explain to the user why your app needs such access.

  2. Verify or request authorization. Use the PHPhotoLibrary requestAuthorization(_:) method to ensure that iOS shows the alert asking for Photos library access at a time that’s appropriate for your app—for example, when the user first opens your app's camera feature. (Don't wait until after the user takes their first photo, as the permission alert interrupts their ability to take multiple photos.)

The code below shows a simple workflow for verifying access:

PHPhotoLibrary.requestAuthorization { status in
    guard status == .authorized else { return }
    
    // Use PHPhotoLibrary.shared().performChanges(...) to add assets.
}

Use a Creation Request to Add a Photo Asset

The AVCapturePhoto fileDataRepresentation() method packages all image data, auxiliary image data, and metadata from the photo capture into a single data object ready for writing to disk. To add that data to the Photos library, use the PHPhotoLibrary and PHAssetCreationRequest classes:

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    guard error == nil else { print("Error capturing photo: \(error!)"); return }

    PHPhotoLibrary.requestAuthorization { status in
        guard status == .authorized else { return }
        
        PHPhotoLibrary.shared().performChanges({
            // Add the captured photo's file data as the main resource for the Photos asset.
            let creationRequest = PHAssetCreationRequest.forAsset()
            creationRequest.addResource(with: .photo, data: photo.fileDataRepresentation()!, options: nil)
        }, completionHandler: self.handlePhotoLibraryError)
    }
}

See Also

Next Steps

Tracking Photo Capture Progress

Monitor key events during capture to provide feedback in your camera UI.

Capturing and Saving Live Photos

Capture Live Photos like those created in the system Camera app and save them to the Photos library.