<!--
{
  "documentType" : "article",
  "framework" : "AVFoundation",
  "identifier" : "/documentation/AVFoundation/saving-captured-photos",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Saving captured photos"
}
-->

# Saving captured photos

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

## Discussion

When you complete a photo capture with [`AVCapturePhotoOutput`](/documentation/AVFoundation/AVCapturePhotoOutput), you receive an [`AVCapturePhoto`](/documentation/AVFoundation/AVCapturePhoto) object that contains the image data, camera metadata, and any auxiliary images you requested, such as thumbnails or depth maps. You can retrieve this data individually from the [`AVCapturePhoto`](/documentation/AVFoundation/AVCapturePhoto) object. Or you can call its [`fileDataRepresentation()`](/documentation/AVFoundation/AVCapturePhoto/fileDataRepresentation()) method to get a <doc://com.apple.documentation/documentation/Foundation/Data> object that’s ready to save using the codec and file format you requested for that photo in [`AVCapturePhotoSettings`](/documentation/AVFoundation/AVCapturePhotoSettings).

After capturing a photo, use <doc://com.apple.documentation/documentation/PhotoKit> to add that data to the user’s photo library.

> Note:
> If your app only needs to access content in the photo library, you can use the <doc://com.apple.documentation/documentation/PhotoKit> Photos picker instead, which doesn’t require you to request access from the user. To learn more, see <doc://com.apple.documentation/documentation/PhotoKit/selecting-photos-and-videos-in-ios>.

### Configure properties and capabilities for your app targets

You can use <doc://com.apple.documentation/documentation/PhotoKit> to enable read/write access to the user’s photo library. To do so, provide a static message for the <doc://com.apple.documentation/documentation/BundleResources/Information-Property-List/NSPhotoLibraryUsageDescription> key in the `Info.plist` file to display to the user when your app requests access.

![A screenshot showing the photo library usage description in the Info tab of the app target. The highlighted string for Privacy Photo Library Usage Description reads This app accesses and saves media in your photo library. ](images/com.apple.avfoundation/media-4132909@2x.png)

In macOS, you also need to enable the <doc://com.apple.documentation/documentation/BundleResources/Entitlements/com.apple.security.personal-information.photos-library> in the Signing & Capabilities tab for your app targets.

![A screenshot showing the macOS Photos Library entitlement enabled in the Signing & Capabilities tab of the app target.](images/com.apple.avfoundation/media-4132908@2x.png)

> Important:
> Your app needs to contain the appropriate key in its `Info.plist` file, and the appropriate entitlement enabled in macOS, before it requests authorization or attempts to use a device. Otherwise, the system terminates your app.

### Request permission to access the user’s photo library

Use the <doc://com.apple.documentation/documentation/Photos/PHPhotoLibrary> <doc://com.apple.documentation/documentation/Photos/PHPhotoLibrary/requestAuthorization(for:handler:)> to request access to the photo library at an appropriate time, such as when the user first opens your app’s camera feature. Here’s an example:

```swift
var isPhotoLibraryReadWriteAccessGranted: Bool {
    get async {
        let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
        
        // Determine if the user previously authorized read/write access.
        var isAuthorized = status == .authorized
        
        // If the system hasn't determined the user's authorization status,
        // explicitly prompt them for approval.
        if status == .notDetermined {
            isAuthorized = await PHPhotoLibrary.requestAuthorization(for: .readWrite) == .authorized
        }
        
        return isAuthorized
    }
}
```

> Note:
> Don’t wait to request access to the photo library until after the user takes their first photo because the permission alert prevents their ability to take multiple photos.

### Use a creation request to add a photo asset

Perform these steps to receive a captured photo and save it to the photo library:

1. Adopt the [`AVCapturePhotoCaptureDelegate`](/documentation/AVFoundation/AVCapturePhotoCaptureDelegate) protocol and implement its [`photoOutput(_:didFinishProcessingPhoto:error:)`](/documentation/AVFoundation/AVCapturePhotoCaptureDelegate/photoOutput(_:didFinishProcessingPhoto:error:)) method to receive a callback for each photo delivered in a capture request.
2. Call [`fileDataRepresentation()`](/documentation/AVFoundation/AVCapturePhoto/fileDataRepresentation()) on the [`AVCapturePhoto`](/documentation/AVFoundation/AVCapturePhoto) object provided by the protocol method to receive a data object containing the photo image data and its attachments, such as camera metadata and auxilliary images.
3. Create a <doc://com.apple.documentation/documentation/Photos/PHAssetCreationRequest> to add the photo resource.

The following code provides an example of this workflow:

```swift
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    if let error {
        print("Error processing photo: \(error.localizedDescription)")
        return
    }
    
    Task {
        await save(photo: photo)
    }
}

func save(photo: AVCapturePhoto) async {
    // Confirm the user granted read/write access.
    guard await isPhotoLibraryReadWriteAccessGranted else { return }
    
    // Create a data representation of the photo and its attachments.
    if let photoData = photo.fileDataRepresentation() {
        PHPhotoLibrary.shared().performChanges {
            // Save the photo data.
            let creationRequest = PHAssetCreationRequest.forAsset()
            creationRequest.addResource(with: .photo, data: photoData, options: nil)
        } completionHandler: { success, error in
            if let error {
                print("Error saving photo: \(error.localizedDescription)")
                return
            }
        }
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)