Add a new Asset to a new asset collection as part of a single change block

Having studied the structure of PHPhotoLibrary, I was able to successfully create and add new objects and collections of images, but the problem I encountered could not successfully create a new collection of assets and add a new Asset for within the same block of changes.

If I create an album as a collection of assets in one change block, and then at the end create an image as an object in another change block, it will work as expected. In addition, if I already have an album and a request for this album, I can add the image as an Asset for this album successfully.

The documentation for the PHAssetCollectionChangeRequest class states:

To add assets to a newly created asset collection or change its title, use the methods listed in the "Modifying Asset Collections" section. To reference a newly created asset collection later in the same change block or after the change block is completed, use the placeholderForCreatedAssetCollection property to retrieve the placeholder object.

I either read it incorrectly, or really have no way to do what he says - to add assets to the newly created collection of assets.

This following code completes “successfully” in the completion handler, but when you enter iOS Photos.app, only an album is created, without adding an image (although the image is added to the camera frame as expected).

, , , PHObjectPlaceholder PHAssetCollection, "", , , ,

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

    // Create the Asset Creation Request to save the photo to the user 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 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);
            }

        }];
    }

}];
+4
1

?

  PHAssetCollection *collection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collectionPlaceholder.localIdentifier] options:nil].firstObject;

  PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
0

Source: https://habr.com/ru/post/1623865/


All Articles