Search code examples
iphoneiosvideoalassetslibrary

Saving Video in an Album Created


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?


Solution

  • 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]);
                                 }];