【问题标题】:Save nsdata(mp4 video) to camera roll将 nsdata(mp4 视频)保存到相机胶卷
【发布时间】:2015-01-06 17:47:57
【问题描述】:

我必须将视频保存到相机胶卷,视频是 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"

【问题讨论】:

    标签: ios video save camera-roll


    【解决方案1】:

    如果你的 VideoPath 是 Like

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

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

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

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

    【讨论】:

      【解决方案2】:
      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 :)");
              }
          }];
      

      【讨论】:

        【解决方案3】:

        我看过一个例子 [这里] (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 格式保存视频。

        【讨论】:

        • 感谢您的快速响应,但我已多次尝试此解决方案,但对我不起作用
        猜你喜欢
        • 2014-02-02
        • 2013-03-21
        • 2013-07-22
        • 2016-05-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-12
        • 2015-09-25
        • 2015-06-11
        相关资源
        最近更新 更多