UISaveVideoAtPathToSavedPhotosAlbum 没有将视频保存到相机胶卷

Posted

技术标签:

【中文标题】UISaveVideoAtPathToSavedPhotosAlbum 没有将视频保存到相机胶卷【英文标题】:UISaveVideoAtPathToSavedPhotosAlbum not saving video to camera roll 【发布时间】:2015-07-29 04:22:10 【问题描述】:

我正在尝试使用glimpse 记录 UIView。它成功地将其保存到应用程序的文档文件夹中,但是我还需要将其保存到用户的相机胶卷中。它没有保存到相机胶卷,我收到一条警告,允许该应用访问我的相机胶卷,但它没有保存在任何相册中。

我已经尝试了相当数量的代码:

[self.glimpse startRecordingView:self.view onCompletion:^(NSURL *fileOuputURL) 
        NSLog(@"DONE WITH OUTPUT: %@", fileOuputURL.absoluteString);


      UISaveVideoAtPathToSavedPhotosAlbum(fileOuputURL.absoluteString,nil,nil,nil);





    ];

至此:

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeVideoAtPathToSavedPhotosAlbum:fileOuputURL
                                    completionBlock:^(NSURL *assetURL, NSError *error)NSLog(@"hello");];

打印日志,但不会将视频保存到相机胶卷。

如果有人对我的这行不通有任何想法,请告诉我! 谢谢!

【问题讨论】:

检查是否可以保存该视频,if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:contentURL]) @ThilinaCháminHewagama 看来视频不兼容我知道了NSLocalizedDescription=The requested URL was not found on this server. 有什么办法可以解决这个问题? 【参考方案1】:

问题在于提供的视频路径。提供来自 url 的相对路径。

if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(fileUrl.relativePath) 
    UISaveVideoAtPathToSavedPhotosAlbum(fileUrl.relativePath, nil, nil, nil)

如果您添加了此 UIVideoAtPathIsCompatibleWithSavedPhotosAlbum,请检查编译器是否会向您显示该文件的问题。

【讨论】:

我可以说UIVideoAtPathIsCompatibleWithSavedPhotosAlbum 在使用absoluteString 报告文件问题。只有当UISaveVideoAtPathToSavedPhotosAlbum 失败时才会注意到问题。但是通过relativePath 可以修复它。日志中的错误是Source file: X is not in /var/mobile/Media nor /var/mobile/Library/ReplayKit nor /var/mobile/Library/SMS/Attachments and the client does not have access【参考方案2】:

问题是您的视频路径以 URL 形式给出,因此您必须检查您的 URL 路径是否可以压缩以保存在视频中,如果您想将视频保存在 camara roll 中,则只需传递 URL 并遵循以下代码:

-(void)saveVideo:(NSString *)videoData withCallBack:(void(^)(id))callBack
library = [[ALAssetsLibrary alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^

    NSData *yourVideoData=[NSData dataWithContentsOfURL:[NSURL URLWithString:videoData]];
    if (yourVideoData) 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"video.mp4"];


                 if([yourVideoData writeToFile:filePath atomically:YES])
                 
                     NSURL *capturedVideoURL = [NSURL URLWithString:filePath];
                    //Here you can check video is compactible to store in gallary or not
                     if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:capturedVideoURL]) 
                         // request to save video in photo roll.
                         [library writeVideoAtPathToSavedPhotosAlbum:capturedVideoURL completionBlock:^(NSURL *assetURL, NSError *error) 
                             if (error) 
                                 callBack(@"error while saving video");
                                 NSLog(@"error while saving video");
                              else

                                     callBack(@"Video has been saved in to album successfully !!!");

                    
                         ];
                     
                

    
);



【讨论】:

以上是关于UISaveVideoAtPathToSavedPhotosAlbum 没有将视频保存到相机胶卷的主要内容,如果未能解决你的问题,请参考以下文章