本文为大地瓜原创,欢迎知识共享,转载请注明出处。
虽然你不注明出处我也没什么精力和你计较。
作者微信号:christgreenlaw
本文是阅读PHAsset文档的学习记录。
文中的蓝色链接,可以调用Xcode直接打开。
PHAsset是Photos Library中的image、video、或者Live Photo的一种表现载体。
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 fetchAssetsInAssetCollection: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.
你要取出assets才能开始工作。使用Fetching Assets 中列出的类方法来获取一个或多个代表了你需要展示或者编辑的资源的
PHAsset
实例。比如说,要取出asset集合(例如album或moment)中的所有asset,调用fetchAssetsInAssetCollection:options: 方法。每个fetch方法都接受 PHFetchOptions参数,利用此参数你可以指明你想retrieve哪些asset,如何分类。
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.)
访问或者处理Photos Library需要用户显式地授权。你第一次调用 Fetching Assets中的方法时,Photos自动要求用户授权。(或者,你也可以通过调用 PHPhotoLibrary requestAuthorization: 在你期望的时刻要求用户授权。)
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.
应用的
Info.plist
文件必须为NSPhotoLibraryUsageDescription关键字提供一个值,向用户解释你的应用为什么需要访问Photos。如果这个值不赋值的话,iOS10及以后版本上运行的应用将会崩溃。
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.
Assets contain only metadata.Asset只包含元数据。任何已有的asset所代表的底层的image或者video数据可能根本就不存在本地设备上。然而,根据你想怎么使用这个数据,你可能不需要完全下载asset数据。比如说,如果你需要用thumbnail images生成一个collection view,那么Photos框架可以管理每个asset的下载、生成、以及缓存。更多细节参见 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 objects are immutable.asset对象是不可更改的。要编辑一个asset的元数据(比如说将其标记为favorite photo),需要在一个photo library change block中创建一个PHAssetChangeRequest对象。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.
对于每一个photo asset,Photos 为其image数据保留一份之前的版本和一份现在的版本,也会保留一份PHAdjustmentData对象,这个对象是描述用户上一次对每个asset内容所做的修改的。
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.)
你开始编辑一个asset时,Photos首先询问你的应用能否从最近的编辑中翻译出adjustment data。如果可以的话,Photos为你的editing session提供一份asset之前的版本作为输入。你读取了adjustment data并且重建了其描述的编辑后,你的应用就可以让用户来修改或者回滚上一次编辑或者做更多的修改。(比如说,adjustment data可以描述对photo所做的筛选。你的应用重新进行这个筛选,允许用户修改筛选参数,添加新的筛选,或者移除筛选。)
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.
如果你的应用不支持asset的adjustment data,Photos将会为你的editing session提供asset的当前版本作为输入。当前版本包含了全部做过的编辑所形成的输出,所以你的应用可以接下来编辑asset但是无法修改或回滚最近的编辑。
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.
对于video asset来说,Photos并不会存储之前的版本。如果你的应用不能读取video asset 的adjustment data,你必须用当前的video版本进行操作,接下来的编辑也不能利用应用的adjustment data。
To edit a PHAsset object’��s photo or video content:
Call the asset’s requestContentEditingInputWithOptions:completionHandler: method. The PHContentEditingInputRequestOptions object you provide for the
options
parameter controls whether your app can handle the asset’s adjustment data.Photos calls your
completionHandler
block, providing a PHContentEditingInput object 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 PHAdjustmentData object describing the changes.
Initialize a PHContentEditingOutput object. For photo- or video-only assets, use the editing output’s properties to provide edited asset data. For Live Photo assets, create a PHLivePhotoEditingContext object to edit the Live Photo content.
Commit your edits to the photo library by posting a change block to the shared PHPhotoLibrary object. In the block, create a PHAssetChangeRequest object and set its contentEditingOutput property to the editing output that you created.
Each PHPhotoLibrary performChanges
call prompts the user for permission to edit the contents of the photo library—to edit multiple assets in one batch, create multiple PHAssetChangeRequest
objects within the same change block, each with its own corresponding PHContentEditingOutput object.