gpt4 book ai didi

ios - 在一个更改 block 中将新 Assets 添加到新 Assets 集合

转载 作者:可可西里 更新时间:2023-11-01 06:13:57 24 4
gpt4 key购买 nike

查看 PHPhotoLibrary 框架后,我已经能够成功创建和添加新的图像 Assets 和集合,但我遇到的问题是无法成功创建新的 Assets 集合并将新的 Assets 添加到在同一个更改 block 中。

如果我在一个更改 block 中创建一个相册作为 Assets 集合,然后在完成后,在另一个更改 block 中创建一个图像作为 Assets ,它会按预期工作。此外,如果我已经有一个相册并查询该相册,我可以成功地将图像作为 Assets 添加到该相册。

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.

我要么误读了它,要么它实际上没有能力按照它所说的那样做——将 Assets 添加到新创建的 Assets 集合中。

下面的代码在完成处理程序中“成功”完成,但是当进入 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 工作的代码:

__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 - 在一个更改 block 中将新 Assets 添加到新 Assets 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34736154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com