在创建的相册中保存视频

时间:2013-07-10 11:29:24

标签: iphone ios video alassetslibrary

我在AppDelegate方法中使用此代码创建了一个专辑

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

现在我想将录制的视频保存到这个创建的999Videos专辑中。不是我这样做过的photosAlbum。

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

视频正在保存但不在999Videos相册中。有人可以告诉我如何将视频保存到我的自定义相册中?

1 个答案:

答案 0 :(得分:14)

在撕掉我的头发后,我终于找到了解决方案。这是我的代码。

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