本文為大地瓜原創(chuàng)泣栈,歡迎知識共享边琉,轉(zhuǎn)載請注明出處倘屹。
雖然你不注明出處我也沒什么精力和你計較。
作者微信號:christgreenlaw
本文是閱讀PHAsset文檔的學(xué)習(xí)記錄厂榛。
文中的藍色鏈接盖矫,可以調(diào)用Xcode直接打開。
PHAsset是Photos Library中的image击奶、video辈双、或者Live Photo的一種表現(xiàn)載體。
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痰驱,調(diào)用fetchAssetsInAssetCollection:options: 方法证芭。每個fetch方法都接受 PHFetchOptions參數(shù),利用此參數(shù)你可以指明你想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需要用戶顯式地授權(quán)。你第一次調(diào)用 Fetching Assets中的方法時另萤,Photos自動要求用戶授權(quán)湃密。(或者诅挑,你也可以通過調(diào)用 PHPhotoLibrary requestAuthorization: 在你期望的時刻要求用戶授權(quán)。)
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.
應(yīng)用的
Info.plist
文件必須為NSPhotoLibraryUsageDescription關(guān)鍵字提供一個值泛源,向用戶解釋你的應(yīng)用為什么需要訪問Photos拔妥。如果這個值不賦值的話,iOS10及以后版本上運行的應(yīng)用將會崩潰达箍。
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只包含元數(shù)據(jù)没龙。任何已有的asset所代表的底層的image或者video數(shù)據(jù)可能根本就不存在本地設(shè)備上。然而缎玫,根據(jù)你想怎么使用這個數(shù)據(jù)硬纤,你可能不需要完全下載asset數(shù)據(jù)。比如說赃磨,如果你需要用thumbnail images生成一個collection view筝家,那么Photos框架可以管理每個asset的下載、生成邻辉、以及緩存溪王。更多細(xì)節(jié)參見 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的元數(shù)據(jù)(比如說將其標(biāo)記為favorite photo)值骇,需要在一個photo library change block中創(chuàng)建一個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數(shù)據(jù)保留一份之前的版本和一份現(xiàn)在的版本道伟,也會保留一份PHAdjustmentData對象,這個對象是描述用戶上一次對每個asset內(nèi)容所做的修改的搅裙。
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首先詢問你的應(yīng)用能否從最近的編輯中翻譯出adjustment data裹芝。如果可以的話部逮,Photos為你的editing session提供一份asset之前的版本作為輸入。你讀取了adjustment data并且重建了其描述的編輯后嫂易,你的應(yīng)用就可以讓用戶來修改或者回滾上一次編輯或者做更多的修改兄朋。(比如說,adjustment data可以描述對photo所做的篩選怜械。你的應(yīng)用重新進行這個篩選颅和,允許用戶修改篩選參數(shù),添加新的篩選缕允,或者移除篩選峡扩。)
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.
如果你的應(yīng)用不支持asset的adjustment data,Photos將會為你的editing session提供asset的當(dāng)前版本作為輸入障本。當(dāng)前版本包含了全部做過的編輯所形成的輸出教届,所以你的應(yīng)用可以接下來編輯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并不會存儲之前的版本案训。如果你的應(yīng)用不能讀取video asset 的adjustment data买置,你必須用當(dāng)前的video版本進行操作,接下來的編輯也不能利用應(yīng)用的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.