在查看PHPhotoLibrary框架之后,我已经能够成功创建和添加新的图像资产和集合,但我遇到的问题是无法成功创建新的资产集合并添加新资产到在同一个变更块内。
如果我在一个更改块中创建一个专辑作为资产集合,然后在完成时,在另一个更改块中创建一个图像作为资产,它按预期工作。此外,如果我已有专辑并查询该专辑,我可以成功将图像作为资产添加到该专辑。
PHAssetCollectionChangeRequest Class Documentation州:
要将资产添加到新创建的资产集合或更改其标题,请使用修改资产集合中列出的方法。要在稍后的同一个更改块中或在更改块完成后引用新创建的资产集合,请使用placeholderForCreatedAssetCollection属性来检索占位符对象。
我要么误读它,要么实际上没有能力按照它的说法去做 - 将资产添加到新创建的资产集合中。
以下代码在完成处理程序中“成功”完成,但进入iOS Photos.app时,仅创建了相册,未添加任何图像(尽管图像按预期添加到相机胶卷中)。 / p>
造成这个问题的原因是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);
}
}];
}
}];
答案 0 :(得分:0)
您是否尝试将相册的占位符转换为一个更改请求中的相册?
PHAssetCollection *collection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collectionPlaceholder.localIdentifier] options:nil].firstObject;
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];