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
charisma98 committed Apr 16, 2021
1 parent 43050df commit 7d2d342
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions ios/Classes/core/PMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -473,23 +473,24 @@ - (void)fetchFullSizeVideo:(PHAsset *)asset handler:(NSObject <PMResultHandler>
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 @@ -501,35 +502,36 @@ - (void)fetchFullSizeVideo:(PHAsset *)asset handler:(NSObject <PMResultHandler>
}];

[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:^{
[handler reply:path];
}];

[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 7d2d342

Please sign in to comment.