Saving Video in an Album Created

2019-02-04 22:31发布

i have created an album using this code in AppDelegate methode

NSString *albumName=@"999Videos";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:albumName
                         resultBlock:^(ALAssetsGroup *group) {
                             NSLog(@"added album:%@", albumName);
                         }
                        failureBlock:^(NSError *error) {
                            NSLog(@"error adding album");
                        }];

Now i want to save the recorded videos to this 999Videos album created.Not to the photosAlbum which i have done like this.

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputFileURL])
    {
        [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
                                    completionBlock:^(NSURL *assetURL, NSError *error)

The videos are saving but not in the 999Videos album.Could someone please tell me how can i save the videos to my custom album?

1条回答
家丑人穷心不美
2楼-- · 2019-02-04 23:02

After tearing my hair out over this finally i found the solution.Here is my code.

NSString *albumName=@"999 Videos";
                ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                [library addAssetsGroupAlbumWithName:albumName
                                         resultBlock:^(ALAssetsGroup *group) {
                                             NSLog(@"added album:%@", albumName);
                                            }
                                        failureBlock:^(NSError *error) {
                                            NSLog(@"error adding album");

                                        }];

__block ALAssetsGroup* groupToAddTo;
    [library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                                    usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                                            if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:albumName]) {
                                                    NSLog(@"found album %@", albumName);
                                                    groupToAddTo = group;
                                                }
                                            }
                                          failureBlock:^(NSError* error) {
                                              NSLog(@"failed to enumerate albums:\nError: %@", [error localizedDescription]);
                                          }];


                [library assetForURL:assetURL
                              resultBlock:^(ALAsset *asset) {
                                  // assign the photo to the album
                                  [groupToAddTo addAsset:asset];
                                  NSLog(@"Added %@ to %@", [[asset defaultRepresentation] filename], albumName);
                              }
                             failureBlock:^(NSError* error) {
                                 NSLog(@"failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);
                             }];
查看更多
登录 后发表回答