Exporting video using PhotoKit (PHAsset) gives different video file every time

独自空忆成欢 提交于 2019-12-18 18:04:10

问题


I use the method (a the end of this question) to retrieve video from the device. What it does, it finds the first video in the library, creates export session and exports the video into MOV file.

After two runs of the application (stopping the app between method runs), two resulting files are being compared. Both files are different. I was expecting that both files would be the same, as the same asset is being exported.

One more remark: running the method twice in the same application run gives me two identical files as expected.

Is it possible to make PhotoKit to export the same file every time it runs?

- (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);
         }];
     }];
}

回答1:


UPDATED

It's not clear but I think exporting video from the camera roll does not guarantee fetching same video in every time. So I copied the video from camera roll to my document folder with url (avurlasset.URL) by [NSFileManager copyItemAtURL:toURL:error:] then it copies the same video file in every time. For now it is my final solution.

In this case you have to use requestAVAssetForVideo not requestExportSessionForVideo

So in your case,

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");
     }
 }];


来源:https://stackoverflow.com/questions/38196661/exporting-video-using-photokit-phasset-gives-different-video-file-every-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!