Saving Video in an Album Created

故事扮演 提交于 2019-11-30 13:22:30

问题


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:


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


来源:https://stackoverflow.com/questions/17569505/saving-video-in-an-album-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!