简体   繁体   English

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

[英]Save nsdata(mp4 video) to camera roll

I have to save video to camera roll , video is in the form of nsdata . 我必须将视频保存到相机胶卷,视频是nsdata的形式。 I know that is the method 我知道那是方法

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

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

Also tried this but not working for me 也尝试过这个但对我不起作用

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

This method accept path not nsdata . 此方法接受的路径不是nsdata。 what should i do? 我该怎么办?

here Video path = @"/var/mobile/Applications/AE75E729-7F10-478B-9DAF-E730EB4231D1/Documents/Videos/12.mp4" 这里的视频路径= @“ / var / mobile / Applications / AE75E729-7F10-478B-9DAF-E730EB4231D1 / Documents / Videos / 12.mp4”

I have seen a example [here] ( https://github.com/versluis/Video-Recorder ) 我在[这里]看过一个示例( https://github.com/versluis/Video-Recorder

I am not tried it, but hope it will help you. 我没有尝试过,但是希望它能对您有所帮助。

Download the code, and see the code in ViewController.m 下载代码,然后在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);

First create a file with NSData, and then path of that file is passed in the method UISaveVideoAtPathToSavedPhotosAlbum. 首先使用NSData创建一个文件,然后在方法UISaveVideoAtPathToSavedPhotosAlbum中传递该文件的路径。

Here is also another link of [stackoverflow answer] ( save mp4 video to device camera roll ), that save video in .mp4 format. 这也是[stackoverflow答案]( 将mp4视频保存到设备相机胶卷 )的另一个链接,该链接将视频保存为.mp4格式。

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

if ur VideoPath is Like 如果您的VideoPath喜欢

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

then convert video path to NSData in this way 然后以这种方式将视频路径转换为NSData

 NSError* error = nil;
 NSData *uploadFileData = [NSData dataWithContentsOfFile:UploadfilePathString  options:0 error:&error];
    NSLog(@"Data read from %@ with error: %@", uploadFileData, error);

output:it comes in data format then simply upload Data format to server . 输出:它采用数据格式,然后只需将数据格式上传到服务器即可。 Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM