将nsdata(mp4视频)保存到相机胶卷

时间:2014-11-10 12:11:31

标签: ios video save camera-roll

我必须将视频保存到相机胶卷,视频采用nsdata的形式。 我知道那是方法

UISaveVideoAtPathToSavedPhotosAlbum(videopath, self,  @selector(video:didFinishSavingWithError:contextInfo:), nil);
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

    NSLog(@"is error?%@",error);
}

也试过这个但不适合我

 NSURL *movieURL = [NSURL fileURLWithPath:self.videoPath];

        NSLog(@"movie url== %@",self.videoPath);
                    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
                 [library writeVideoAtPathToSavedPhotosAlbum:movieURL
                                             completionBlock:^(NSURL *assetURL, NSError *error){NSLog(@"complete ");}];

此方法接受路径而不是nsdata。我该怎么办?

此处视频路径= @" /var/mobile/Applications/AE75E729-7F10-478B-9DAF-E730EB4231D1/Documents/Videos/12.mp4"

3 个答案:

答案 0 :(得分:0)

我见过一个例子[这里](https://github.com/versluis/Video-Recorder

我没有尝试过,但希望它会对你有帮助。

下载代码,并查看ViewController.m中的代码

NSURL *chosenMovie = [info objectForKey:UIImagePickerControllerMediaURL];

// save it to the documents directory
NSURL *fileURL = [self grabFileURL:@"video.mov"];
NSData *movieData = [NSData dataWithContentsOfURL:chosenMovie];
[movieData writeToURL:fileURL atomically:YES];

// save it to the Camera Roll
UISaveVideoAtPathToSavedPhotosAlbum([chosenMovie path], nil, nil, nil);

首先使用NSData创建一个文件,然后在方法UISaveVideoAtPathToSavedPhotosAlbum中传递该文件的路径。

以下是[stackoverflow answer](save mp4 video to device camera roll)的另一个链接,它以.mp4格式保存视频。

答案 1 :(得分:0)

NSData *video_data=//Your Video NSData
    // Write it to cache directory
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file1.mp4"];
    [video_data writeToFile:path atomically:YES];
    NSLog(@"Path:%@",path);
    // After that use this path to save it to PhotoLibrary
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError
*error) {
        if (error) {
            NSLog(@"%@", error.description);
        }else {
            NSLog(@"Done :)");
        }
    }];

答案 2 :(得分:0)

如果您的VideoPath相似

<img src="yourImgeName.png" />

然后以这种方式将视频路径转换为NSData

 UploadfilePathString = @"/var/mobile/Applications/AE75E729-7F10-478B-9DAF-E730EB4231D1/Documents/Videos/12.mp4"

输出:它以数据格式出现 然后只需将数据格式上传到服务器。 谢谢