将视频从 Documents 目录存储到相册

Posted

技术标签:

【中文标题】将视频从 Documents 目录存储到相册【英文标题】:Store the video from Documents directory to Photos album 【发布时间】:2014-12-15 20:38:12 【问题描述】:

我已将视频临时存储在 Documents 目录中。

这是示例视频路径:file:///var/mobile/Containers/Data/Application/C651A13F-8F7C-4FCB-A1D9-D6D4C5F512E7/Documents/mergeVideo.mov

我想将此视频保存在专辑目录中。为此我必须将此文件 url 转换为资产 url。

我已经尝试了解决方法..但无济于事..有人可以指导..吗?

要使这段代码工作,我必须将文件 url 转换为资产 url .. 怎么做?

我已经有了使用资产 url 保存视频的代码。这是我的代码:

-(void)save2cameraRoll


NSString *albumName=@"album name";
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]);
                     ];

NSURL *fileURL = videoInputUrl;


[library assetForURL:fileURL
         resultBlock:^(ALAsset *asset) 
             // assign the photo to the album
             [groupToAddTo addAsset:asset];
             NSLog(@"Added %@ to %@", [[asset defaultRepresentation] filename], albumName);
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Video Saved to HubRamped Album."  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
             [alert show];
         
        failureBlock:^(NSError* error) 
            NSLog(@"failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);
        ];


【问题讨论】:

【参考方案1】:

这是保存视频并将该视频添加到自定义相册中的代码 sn-p ..

 [library writeVideoAtPathToSavedPhotosAlbum:videoInputUrl completionBlock:^(NSURL *assetURL, NSError *error)
    if (error) 

        NSLog(@"Final Video could not be saved");
    
    else

            [library assetForURL:assetURL
                     resultBlock:^(ALAsset *asset) 
                         // assign the photo to the album
                         [groupToAddTo addAsset:asset];
                         NSLog(@"Added %@ to %@", [[asset defaultRepresentation] filename], albumName);
                         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Video Saved to HubRamped Album."  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
                         [alert show];
                     
                    failureBlock:^(NSError* error) 
                        NSLog(@"failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);
                    ];
    
];

【讨论】:

以上是关于将视频从 Documents 目录存储到相册的主要内容,如果未能解决你的问题,请参考以下文章

iOS 文件路径及数据保存

从图像创建视频并保存到相册

如何将我的视频保存到相册中的特殊文件夹?

iOS视频压缩存储至本地并上传至服务器

iOS视频压缩存储至本地并上传至服务器-b

从支持文件中的图像目录获取图像路径