在一个变更块内将新资产添加到新资产集合
Posted
技术标签:
【中文标题】在一个变更块内将新资产添加到新资产集合【英文标题】:Add new Asset to a new Asset Collection within one Change Block 【发布时间】:2016-01-12 05:24:51 【问题描述】:查看 phphotoLibrary 框架后,我已经能够成功创建和添加新的图像资产和集合,但我遇到的问题是无法成功创建新的资产集合并添加新的资产到在同一个更改块中。
如果我在一个更改块中创建一个相册作为资产集合,然后在完成后,在另一个更改块中创建一个图像作为资产,它会按预期工作。此外,如果我已经有一个相册并查询该相册,我可以成功地将图像作为资产添加到该相册。
PHAssetCollectionChangeRequest Class Documentation 声明:
要将资产添加到新创建的资产集合或更改其标题,请使用修改资产集合中列出的方法。要稍后在同一更改块中或更改块完成后引用新创建的资产集合,请使用 placeholderForCreatedAssetCollection 属性检索占位符对象。
我要么误读了它,要么它实际上并没有能力按照它说的去做——将资产添加到新创建的资产集合中。
以下代码在完成处理程序中“成功”完成,但进入 ios Photos.app 时,仅创建相册,没有添加图像(尽管图像已按预期添加到相机胶卷)。
导致问题的原因是 PHObjectPlaceholder 不能用作 PHAssetCollection,所以他们所说的“参考”不能以这种方式使用,所以这是我无法理解的根本问题:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
// Create the Asset Creation Request to save the photo to the user's photos - This will add it to the camera roll at the very least
PHAssetChangeRequest *imageCreationRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// Create the Asset Collection Creation Request to create the new album - This will create the album at the very least
PHAssetCollectionChangeRequest *creationRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"New Album"];
PHObjectPlaceholder *collectionPlaceholder = creationRequest.placeholderForCreatedAssetCollection; // Get the placeholder Asset Collection
// Create the Asset Collection Change Request to add the new image Asset to the new album Asset Collection
// Warns about PHObjectPlaceholder* != PHAssetCollection*
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collectionPlaceholder];
[albumChangeRequest addAssets:@[imageCreationRequest.placeholderForCreatedAsset]];
completionHandler:^(BOOL success, NSError * _Nullable error)
if (success)
NSLog(@"Saved to iOS Photos after creating album");
];
如果有帮助,这是使用两个更改块的代码:
__block PHObjectPlaceholder *collectionPlaceholder;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
// Create the Asset Collection Creation Request to create the new album - This will create the album at the very least
PHAssetCollectionChangeRequest *creationRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"New Album"];
collectionPlaceholder = creationRequest.placeholderForCreatedAssetCollection; // Get the placeholder Asset Collection
completionHandler:^(BOOL success, NSError * _Nullable error)
if (success)
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
// Create the Asset Creation Request to save the photo to the user's photos - This will add it to the camera roll at the very least
PHAssetChangeRequest *imageCreationRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// Get the album collection using the placeholder identifier from the first change block
PHAssetCollection *collection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collectionPlaceholder.localIdentifier] options:nil].firstObject;
// Create the Asset Collection Change Request to add the new image Asset to the new album Asset Collection
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
[albumChangeRequest addAssets:@[imageCreationRequest.placeholderForCreatedAsset]];
completionHandler:^(BOOL success, NSError * _Nullable error)
if (success)
NSLog(@"Saved to iOS Photos after creating album");
else
NSLog(@"Couldn't save to iOS Photos after creating album (%@)", error.description);
];
];
【问题讨论】:
您找到解决方案了吗? @PiersGeyman 老实说,自去年 8 月以来,我没有尝试过任何新的东西。考虑到这又回到了 iOS 8 中(根据我的标签,我不记得我为什么添加了),也许有一个更新允许你这样做,但是如果你最近尝试过并且结果相同,那么它可能只是怎么样。我解决了我的问题。 任何人回到这个,我已经放弃了占位符废话,只是把它分成多个更改块。我什至不交出对收藏的引用。每次都查一下就可以了。这个 API 是我见过的来自苹果的最草率、文档记录最差、最笨拙、最蹩脚、质量低下的东西,而且很愚蠢。 【参考方案1】:您是否尝试过在一个更改请求中将相册的占位符转换为相册?
PHAssetCollection *collection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collectionPlaceholder.localIdentifier] options:nil].firstObject;
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
【讨论】:
我不记得我在发布之前是否尝试过这个,但我现在只是尝试这样做,它也返回一个错误(甚至没有将照片添加到相机胶卷)。 多年后,这整个模型仍然是一个杂乱无章的火车残骸,几乎不值得使用。 :(以上是关于在一个变更块内将新资产添加到新资产集合的主要内容,如果未能解决你的问题,请参考以下文章