9

我在 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 相册中。而不是我这样做的照片相册。

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

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

4

1 回答 1

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]);
                             }];
于 2013-07-10T14:56:52.480 回答