PHAsset

本文为大地瓜原创,欢迎知识共享,转载请注明出处。
虽然你不注明出处我也没什么精力和你计较。
作者微信号: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:

  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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,417评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,921评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,850评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,945评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,069评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,188评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,239评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,994评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,409评论 1 304
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,735评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,898评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,578评论 4 336
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,205评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,916评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,156评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,722评论 2 363
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,781评论 2 351

推荐阅读更多精彩内容