Skip to content

Commit

Permalink
fix iCloud video export session error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JimmyTai committed Apr 15, 2021
1 parent f1097d6 commit 9771626
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions ios/Classes/core/PMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -478,23 +478,24 @@ - (void)fetchFullSizeVideo:(PHAsset *)asset handler:(ResultHandler *)handler pro
attributes:@{}
error:nil];

[path appendFormat:@"%@/%@", @".video", filename];
[path appendFormat:@"%@/%d_%@", @".video", (int)asset.modificationDate.timeIntervalSince1970 ,filename];

PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.version = PHVideoRequestOptionsVersionCurrent;
if ([manager fileExistsAtPath:path]) {
[[PMLogUtils sharedInstance]
info:[NSString stringWithFormat:@"read cache from %@", path]];
[handler reply:path];
return;
}



[self notifyProgress:progressHandler progress:0 state:PMProgressStatePrepare];
[options setProgressHandler:^(double progress, NSError *error, BOOL *stop,
NSDictionary *info) {
NSDictionary *info) {
if (progress == 1.0) {
[self fetchFullSizeVideo:asset handler:handler progressHandler:nil];
[self notifyProgress:progressHandler progress:progress state:PMProgressStateLoading];
}

if (error) {
[self notifyProgress:progressHandler progress:progress state:PMProgressStateFailed];
[progressHandler deinit];
Expand All @@ -506,41 +507,36 @@ - (void)fetchFullSizeVideo:(PHAsset *)asset handler:(ResultHandler *)handler pro
}];

[options setNetworkAccessAllowed:YES];

[[PHImageManager defaultManager]
requestAVAssetForVideo:asset
options:options
resultHandler:^(AVAsset *_Nullable asset,
AVAudioMix *_Nullable audioMix,
NSDictionary *_Nullable info) {
BOOL downloadFinish = [PMManager isDownloadFinish:info];

if (!downloadFinish) {
return;
}

NSString *preset = AVAssetExportPresetHighestQuality;
AVAssetExportSession *exportSession =
[AVAssetExportSession exportSessionWithAsset:asset
presetName:preset];
if (exportSession) {
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = [NSURL fileURLWithPath:path];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if ([exportSession status] == AVAssetExportSessionStatusCompleted) {
[handler reply:path];
}else if ([exportSession status] == AVAssetExportSessionStatusCancelled) {
[handler reply:nil];
}else if ([exportSession status] == AVAssetExportSessionStatusFailed) {
[handler reply:nil];
}
}];

[self notifySuccess:progressHandler];
} else {
[handler reply:nil];
}
}];
requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession *_Nullable exportSession, NSDictionary *_Nullable info) {
BOOL downloadFinish = [PMManager isDownloadFinish:info];

if (!downloadFinish) {
NSLog(@"Asset download fail: %@");
[handler reply:nil];
return;
}

if (exportSession) {
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = [NSURL fileURLWithPath:path];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if ([exportSession status] == AVAssetExportSessionStatusCompleted) {
[handler reply:path];
} else if ([exportSession status] == AVAssetExportSessionStatusFailed) {
NSLog(@"Export session failed: %@", exportSession.error);
[handler reply:nil];
} else if ([exportSession status] == AVAssetExportSessionStatusCancelled) {
NSLog(@"Export session cancelled: %@", exportSession.error);
[handler reply:nil];
}
}];
[self notifySuccess:progressHandler];
} else {
[handler reply:nil];
}
}];
}

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

0 comments on commit 9771626

Please sign in to comment.