PHAsset

本文為大地瓜原創(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:

  1. 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.

  2. Photos calls your completionHandler block, providing a PHContentEditingInput object you can use for retrieving the image or video data to be edited.

  3. 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.

  4. 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.

  5. 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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末忿项,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子城舞,更是在濱河造成了極大的恐慌轩触,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件家夺,死亡現(xiàn)場離奇詭異怕膛,居然都是意外死亡,警方通過查閱死者的電腦和手機秦踪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進店門褐捻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人椅邓,你說我怎么就攤上這事柠逞。” “怎么了景馁?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵板壮,是天一觀的道長。 經(jīng)常有香客問我合住,道長绰精,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任透葛,我火速辦了婚禮笨使,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘僚害。我一直安慰自己硫椰,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布萨蚕。 她就那樣靜靜地躺著靶草,像睡著了一般。 火紅的嫁衣襯著肌膚如雪岳遥。 梳的紋絲不亂的頭發(fā)上奕翔,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天,我揣著相機與錄音浩蓉,去河邊找鬼派继。 笑死帮坚,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的互艾。 我是一名探鬼主播试和,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼纫普!你這毒婦竟也來了阅悍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤昨稼,失蹤者是張志新(化名)和其女友劉穎节视,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體假栓,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡寻行,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了匾荆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拌蜘。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖牙丽,靈堂內(nèi)的尸體忽然破棺而出简卧,到底是詐尸還是另有隱情,我是刑警寧澤烤芦,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布举娩,位于F島的核電站,受9級特大地震影響构罗,放射性物質(zhì)發(fā)生泄漏铜涉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一遂唧、第九天 我趴在偏房一處隱蔽的房頂上張望芙代。 院中可真熱鬧,春花似錦蠢箩、人聲如沸链蕊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至逻谦,卻和暖如春掌实,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背邦马。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工贱鼻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留宴卖,地道東北人。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓邻悬,卻偏偏與公主長得像症昏,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子父丰,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容