iOS 7视频保存问题

3

我将视频保存到照片库中。它可以在先前的iOS版本中工作,在iOS 7中,videoAtPathIsCompatibleWithSavedPhotosAlbum这一行总是为false。

这是我的代码,请帮助我。

NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"NewmergeVideo.mov"];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPathDocs];
if (fileExists) {
    [[NSFileManager defaultManager] removeItemAtPath: myPathDocs error:NULL];

}

NSURL *url = [NSURL fileURLWithPath:myPathDocs];
NSLog(@"%@",url);
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL=url;
//[exporter setVideoComposition:MainCompositionInst];
exporter.outputFileType = AVFileTypeQuickTimeMovie;

[exporter exportAsynchronouslyWithCompletionHandler:^
 {
     dispatch_async(dispatch_get_main_queue(), ^{
         [self _exportDidFinish:exporter];

     });
 }];

这里是保存代码

- (void)_exportDidFinish:(AVAssetExportSession*)session
{ 
    NSURL *outputURL = session.outputURL;
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    NSLog(@"%@",outputURL);

    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:outputURL]) {
        [library writeVideoAtPathToSavedPhotosAlbum:outputURL
                                    completionBlock:^(NSURL *assetURL, NSError *error){
                                        dispatch_async(dispatch_get_main_queue(), ^{
                                            if (error) {
                                                NSLog(@"writeVideoToAssestsLibrary failed: %@", error);

                                            }else{
                                                NSLog(@"Writing3");

                                            }

                                        });

                                    }];
    }
    [library release];
}

在之前的iOS中,NSURL表示为:file://localhost/var/mobile/Applications/99B72CBA-A426-4F04-B7B2-2B61F0B0C513/Documents/NewmergeVideo.mov

而在iOS 7中,NSURL表示为:file:///var/mobile/Applications/791244EE-771B-46C9-BD57-BA0BE6CACD3C/Documents/NewmergeVideo.mov


仍然遇到同样的问题。正在努力解决。 - Jim Jones
发现了错误,它不是文件路径错误。请检查您的最终视频设置。在我的代码中,我不小心将timeScale = 600,现在我将其更改为30,现在它可以正常工作了。 - mychar
你要设置哪个对象或调用哪个方法来设置timeScale?我正在使用以下代码: CMTimeRangeMake(kCMTimeZero,videoAsset.duration); - Jim Jones
MainCompositionInst.frameDuration = CMTimeMake(1,TIME_SCALE); 主合成对象帧持续时间 = CMTimeMake(1,TIME_SCALE); - mychar
非常感谢,你刚刚救了我!!!!! - Marmik
很高兴听到它对其他人有效。 - mychar
1个回答

0

您可以使用此代码保存文件。(它可以正常工作)

 if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(outputURL))
{
      UISaveVideoAtPathToSavedPhotosAlbum(outputURL, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}

-(void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    if(error)
        NSLog(@"Finished saving video with error: %@", error);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接