Add the image and other data from a photo capture to the Photos library.
When you complete a photo capture with
AVCapture, you receive an
AVCapture 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
AVCapture, or simply call its
file method to get a
Data object ready for writing to disk, using the codec and file format you requested for that photo in
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:
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.
Verify or request authorization. Use the
requestmethod 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: