Skip to content

Commit 429a5b3

Browse files
author
JimmyTai
committed
fix iCloud video export session error
1. Fix fetchFullSizeVideo flow logic. Avoid keeping call fetchFullSizeVideo when progress is 1.0. This operation cause a loop. 2. Change requestAVAssetForVideo to requestExportSessionForVideo. Avoid iCloud file export failure. Ref: banchichen/TZImagePickerController#1073 3. Add modification date as file name prefix. Avoid to get the cache file if user modified it.
1 parent 3d72be4 commit 429a5b3

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

ios/Classes/core/PMManager.m

+36-34
Original file line numberDiff line numberDiff line change
@@ -473,23 +473,24 @@ - (void)fetchFullSizeVideo:(PHAsset *)asset handler:(NSObject <PMResultHandler>
473473
attributes:@{}
474474
error:nil];
475475

476-
[path appendFormat:@"%@/%@", @".video", filename];
476+
[path appendFormat:@"%@/%d_%@", @".video", (int)asset.modificationDate.timeIntervalSince1970 ,filename];
477+
477478
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
479+
options.version = PHVideoRequestOptionsVersionCurrent;
478480
if ([manager fileExistsAtPath:path]) {
479481
[[PMLogUtils sharedInstance]
480482
info:[NSString stringWithFormat:@"read cache from %@", path]];
481483
[handler reply:path];
482484
return;
483485
}
484-
485-
486+
486487
[self notifyProgress:progressHandler progress:0 state:PMProgressStatePrepare];
487488
[options setProgressHandler:^(double progress, NSError *error, BOOL *stop,
488-
NSDictionary *info) {
489+
NSDictionary *info) {
489490
if (progress == 1.0) {
490-
[self fetchFullSizeVideo:asset handler:handler progressHandler:nil];
491+
[self notifyProgress:progressHandler progress:progress state:PMProgressStateLoading];
491492
}
492-
493+
493494
if (error) {
494495
[self notifyProgress:progressHandler progress:progress state:PMProgressStateFailed];
495496
[progressHandler deinit];
@@ -501,35 +502,36 @@ - (void)fetchFullSizeVideo:(PHAsset *)asset handler:(NSObject <PMResultHandler>
501502
}];
502503

503504
[options setNetworkAccessAllowed:YES];
504-
505505
[[PHImageManager defaultManager]
506-
requestAVAssetForVideo:asset
507-
options:options
508-
resultHandler:^(AVAsset *_Nullable asset,
509-
AVAudioMix *_Nullable audioMix,
510-
NSDictionary *_Nullable info) {
511-
BOOL downloadFinish = [PMManager isDownloadFinish:info];
512-
513-
if (!downloadFinish) {
514-
return;
515-
}
516-
517-
NSString *preset = AVAssetExportPresetHighestQuality;
518-
AVAssetExportSession *exportSession =
519-
[AVAssetExportSession exportSessionWithAsset:asset
520-
presetName:preset];
521-
if (exportSession) {
522-
exportSession.outputFileType = AVFileTypeMPEG4;
523-
exportSession.outputURL = [NSURL fileURLWithPath:path];
524-
[exportSession exportAsynchronouslyWithCompletionHandler:^{
525-
[handler reply:path];
526-
}];
527-
528-
[self notifySuccess:progressHandler];
529-
} else {
530-
[handler reply:nil];
531-
}
532-
}];
506+
requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession *_Nullable exportSession, NSDictionary *_Nullable info) {
507+
BOOL downloadFinish = [PMManager isDownloadFinish:info];
508+
509+
if (!downloadFinish) {
510+
NSLog(@"Asset download fail: %@");
511+
[handler reply:nil];
512+
return;
513+
}
514+
515+
if (exportSession) {
516+
exportSession.shouldOptimizeForNetworkUse = YES;
517+
exportSession.outputFileType = AVFileTypeMPEG4;
518+
exportSession.outputURL = [NSURL fileURLWithPath:path];
519+
[exportSession exportAsynchronouslyWithCompletionHandler:^{
520+
if ([exportSession status] == AVAssetExportSessionStatusCompleted) {
521+
[handler reply:path];
522+
} else if ([exportSession status] == AVAssetExportSessionStatusFailed) {
523+
NSLog(@"Export session failed: %@", exportSession.error);
524+
[handler reply:nil];
525+
} else if ([exportSession status] == AVAssetExportSessionStatusCancelled) {
526+
NSLog(@"Export session cancelled: %@", exportSession.error);
527+
[handler reply:nil];
528+
}
529+
}];
530+
[self notifySuccess:progressHandler];
531+
} else {
532+
[handler reply:nil];
533+
}
534+
}];
533535
}
534536

535537
- (NSString *)makeAssetOutputPath:(PHAsset *)asset isOrigin:(Boolean)isOrigin {

0 commit comments

Comments
 (0)