ios – 将新资产添加到一个更改块中的新资产集合中

ios – 将新资产添加到一个更改块中的新资产集合中,第1张

概述在查看 PHPhotoLibrary框架之后,我已经能够成功创建和添加新的图像资产和集合,但我遇到的问题是无法成功创建新的资产集合并在其中添加新的资产.相同的更改块. 如果我在一个更改块中创建一个专辑作为资产集合,然后在完成时,在另一个更改块中创建一个图像作为资产,它按预期工作.此外,如果我已有专辑并查询该专辑,我可以成功将图像作为资产添加到该专辑. PHAssetCollectionChange 在查看 PHPhotolibrary框架之后,我已经能够成功创建和添加新的图像资产和集合,但我遇到的问题是无法成功创建新的资产集合并在其中添加新的资产.相同的更改块.

如果我在一个更改块中创建一个专辑作为资产集合,然后在完成时,在另一个更改块中创建一个图像作为资产,它按预期工作.此外,如果我已有专辑并查询该专辑,我可以成功将图像作为资产添加到该专辑.

PHAssetCollectionChangeRequest Class Documentation声明:

To add assets to the newly created asset collection or change its Title,use the methods Listed in Modifying Asset Collections. To reference the newly created asset collection later in the same change block or after the change block completes,use the placeholderForCreatedAssetCollection property to retrIEve a placeholder object.

我要么误读它,要么实际上没有能力按照它的说法去做 – 将资产添加到新创建的资产集合中.

以下代码在完成处理程序中“成功”完成,但进入iOS Photos.app时,仅创建了Album,未添加任何图像(尽管图像按预期添加到相机胶卷).

造成这个问题的原因是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);            }        }];    }}];
解决方法 您是否尝试将相册的占位符转换为一个更改请求中的相册?

PHAssetCollection *collection = [PHAssetCollection fetchAssetCollectionsWithLocalIDentifIErs:@[collectionPlaceholder.localIDentifIEr] options:nil].firstObject;  PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
总结

以上是内存溢出为你收集整理的ios – 将新资产添加到一个更改块中的新资产集合中全部内容,希望文章能够帮你解决ios – 将新资产添加到一个更改块中的新资产集合中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1003670.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存