我正在尝试从UIImagePickerController创建的源视频中导出.mov文件。问题在于AVAssetExportSession创建的输出文件只有668个字节。为什么会失败?我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSURL *imagePickerVideoURL = [info objectForKey:UIImagePickerControllerMediaURL];
    NSString *filename = @"vid1.mov";

    AVAsset *video = [AVAsset assetWithURL:imagePickerVideoURL];
    AVAssetExportSession *exportSession
      = [AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPresetMediumQuality];
    exportSession.shouldOptimizeForNetworkUse = YES;
    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
    exportSession.outputURL = [pathToSavedVideosDirectory URLByAppendingPathComponent:filename];
    NSLog(@"processing video...: %@", exportSession);
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
                       NSLog(@"done processing video!");
                   }];
}

最佳答案

我猜这是因为exportSession没有保留在内存中,因此在didFinishPickingMediaWithInfo完成之后(在导出会话完成处理程序运行之前)被杀死了。

exportSession存储为@property或更改您的完成处理程序以复制对exportSession的引用,如下所示:

[exportSession exportAsynchronouslyWithCompletionHandler:^{
       if (AVAssetExportSessionStatusCompleted == exportSession.status)
       {
           NSLog(@"done processing video!");
       }
}];

07-24 09:37