A representation of an image, video or Live Photo in the Photos library.
Language
- Swift
- Objective-C
SDKs
- iOS 8.0+
- tvOS 10.0+
Overview
You fetch assets to begin working with them. Use the class methods listed in Fetching Assets to retrieve one or more PHAsset instances representing the assets you want to display or edit. For example, to fetch all the assets in an asset collection (such as an album or moment), use the fetchAssets(in:options:) method. Each fetch method takes a PHFetchOptions parameter that you can use to specify which assets you want to retrieve and how to sort them.
Important
Accessing or modifying the Photos library requires explicit authorization from the user. The first time you call one of the methods listed in Fetching Assets, Photos automatically prompts the user for authorization. (Alternatively, you can use the PHPhotoLibrary requestAuthorization(_:) method to prompt the user at a time of your choosing.)
Your app’s Info.plist file must provide a value for the NSPhotoLibraryUsageDescription key that explains to the user why your app is requesting Photos access. Apps linked on or after iOS 10.0 will crash if this key is not present.
Assets contain only metadata. The underlying image or video data for any given asset might not be stored on the local device. However, depending on how you plan to use this data, you may not need to download all of it. For example, if you need to populate a collection view with thumbnail images, the Photos framework can manage downloading, generating, and caching thumbnails for each asset. For details, see PHImageManager.
Asset objects are immutable. To edit an asset’s metadata (for example, to mark it as a favorite photo), create a PHAssetChangeRequest object within a photo library change block. For more details on using change requests and change blocks to update the photo library, see PHPhotoLibrary.
Asset Versions and Editing Asset Content
For each photo asset, Photos keeps a previous and a current version of its image data, as well as a PHAdjustmentData object that describes the last edit the user made to each asset’s content. Your app uses this information to support resumable editing.
When you begin editing an asset, Photos first asks whether your app can interpret the adjustment data from the most recent edit. If so, Photos provides the previous version of the asset as input for your editing session. After you read the adjustment data and reconstruct the edit it describes, your app might let the user alter or revert the last edit or make further changes. (For example, adjustment data may describe filters applied to a photo. Your app reapplies those filters and allows the user to change filter parameters, add new filters, or remove filters.)
If your app does not support an asset’s adjustment data, Photos provides the current version of the asset as input to your editing session. The current version contains the rendered output of all past edits, so your app can further edit the asset but cannot alter or revert most recent edit.
For video assets, Photos does not store a previous version. If your app cannot read a video asset’s adjustment data, you must work with the current version of the video and future edits cannot make use of your app’s adjustment data.
To edit a PHAsset object’s photo or video content:
Call the asset’s
requestContentEditingInput(with:completionHandler:)method. ThePHContentEditingInputRequestOptionsobject you provide for theoptionsparameter controls whether your app can handle the asset’s adjustment data.Photos calls your
completionHandlerblock, providing aPHContentEditingInputobject you can use for retrieving the image or video data to be edited.Apply your edits to the asset. To allow a user to continue working with your edits later, create a new
PHAdjustmentDataobject describing the changes.Initialize a
PHContentEditingOutputobject. For photo- or video-only assets, use the editing output’s properties to provide edited asset data. For Live Photo assets, create aPHLivePhotoEditingContextobject to edit the Live Photo content.Commit your edits to the photo library by posting a change block to the shared
PHPhotoLibraryobject. In the block, create aPHAssetChangeRequestobject and set itscontentEditingOutputproperty to the editing output that you created.Each
PHPhotoLibraryperformChangescall prompts the user for permission to edit the contents of the photo library—to edit multiple assets in one batch, create multiplePHAssetChangeRequestobjects within the same change block, each with its own correspondingPHContentEditingOutputobject.