使用PhotoKit(PHAsset)导出video每次都会显示不同的video文件

我使用该方法(此问题的结尾)从设备中检索video。 它做了什么,它找到了库中的第一个video,创建了导出会话并将video导出到MOV文件中。

在两次运行应用程序(在方法运行之间停止应用程序)之后,将比较两个生成的文件。 两个文件都不同。 我期待两个文件都是相同的,因为正在导出相同的资产。

还有一点:在同一个应用程序运行中运行该方法两次,给出了两个相同的文件。

是否可以让PhotoKit在每次运行时导出相同的文件?

- (void)testVideoRetrievalSO { PHAsset *oneVideo = [[PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil] firstObject]; PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init]; options.networkAccessAllowed = YES; options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat; options.version = PHVideoRequestOptionsVersionOriginal; [[PHImageManager defaultManager] requestExportSessionForVideo:oneVideo options:options exportPreset:AVAssetExportPresetPassthrough resultHandler: ^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) { NSLog(@"Video test run on asset %@", oneVideo.localIdentifier); NSString *folderPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; NSString *fileName = [[[NSUUID UUID] UUIDString] stringByAppendingPathExtension:@"mov"]; NSString *tempFile = [folderPath stringByAppendingPathComponent:fileName]; NSURL *tempFileUrl = [NSURL fileURLWithPath:tempFile]; [exportSession setOutputFileType:AVFileTypeQuickTimeMovie]; [exportSession setOutputURL:tempFileUrl]; [exportSession exportAsynchronouslyWithCompletionHandler:^{ NSLog(@"Video test run exported video into file: %@", tempFile); }]; }]; } 

更新

目前尚不清楚,但我认为从相机胶卷输出video并不能保证每次都能获取相同的video。 所以我通过[NSFileManager copyItemAtURL:toURL:error:]将video从相机胶卷复制到我的文档文件夹和url(avurlasset.URL),然后每次都复制相同的video文件。 现在这是我的最终解决方案。

在这种情况下,您必须使用requestAVAssetForVideo而不是requestExportSessionForVideo

所以在你的情况下,

 PHVideoRequestOptions *options = [PHVideoRequestOptions new]; options.version = PHVideoRequestOptionsVersionOriginal; [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler: ^(AVAsset * _Nullable avasset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) { NSError *error; AVURLAsset *avurlasset = (AVURLAsset*) avasset; // Write to documents folder NSURL *fileURL = [NSURL fileURLWithPath:tmpShareFilePath]; if ([[NSFileManager defaultManager] copyItemAtURL:avurlasset.URL toURL:fileURL error:&error]) { NSLog(@"Copied correctly"); } }];