将视频从Documents目录存储到Photos album

时间:2014-12-15 20:38:13

标签: ios objective-c iphone video ios-camera

我已将视频存储在Documents目录中以供临时使用。

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

我想将此视频保存在相册目录中。为此,我必须将此文件网址转换为资源网址。

我已经尝试了解决方法......但是没有帮助解决..任何人都可以指导..?

要使这段代码工作,我必须将文件网址转换为资产网址。如何做到这一点?

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

-(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 个答案:

答案 0 :(得分:0)

以下是保存视频并在自定义相册中添加该视频的代码段。

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