在查看 PHPhotoLibrary 框架后,我已经能够成功创建和添加新的图像资产和集合,但我遇到的问题是无法成功创建新的资产集合并在相同的更改块。
如果我在一个更改块中创建一个相册作为资产集合,然后在完成后,在另一个更改块中创建一个图像作为资产,它会按预期工作。此外,如果我已经有一个相册并查询该相册,我可以成功地将图像作为资产添加到该相册。
PHAssetCollectionChangeRequest类文档指出:
要将资产添加到新创建的资产集合或更改其标题,请使用修改资产集合中列出的方法。要稍后在同一更改块中或更改块完成后引用新创建的资产集合,请使用 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);
}
}];
}
}];