From 429a5b32bcd489b15a75917eff762311b5cf9dbb Mon Sep 17 00:00:00 2001 From: JimmyTai Date: Fri, 16 Apr 2021 15:04:26 +0800 Subject: [PATCH 1/2] 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: https://github.com/banchichen/TZImagePickerController/issues/1073 3. Add modification date as file name prefix. Avoid to get the cache file if user modified it. --- ios/Classes/core/PMManager.m | 70 ++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/ios/Classes/core/PMManager.m b/ios/Classes/core/PMManager.m index 7c9da9e3..1de493cc 100644 --- a/ios/Classes/core/PMManager.m +++ b/ios/Classes/core/PMManager.m @@ -473,23 +473,24 @@ - (void)fetchFullSizeVideo:(PHAsset *)asset handler:(NSObject 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]; @@ -501,35 +502,36 @@ - (void)fetchFullSizeVideo:(PHAsset *)asset handler:(NSObject }]; [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 { From 8c648b1d7746a646ef5d614b10faf08759f17fed Mon Sep 17 00:00:00 2001 From: JimmyTai Date: Fri, 16 Apr 2021 15:04:47 +0800 Subject: [PATCH 2/2] avoid loop calling for fetchFullSizeImageFile --- ios/Classes/core/PMManager.m | 40 +++++++++++++++++------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/ios/Classes/core/PMManager.m b/ios/Classes/core/PMManager.m index 1de493cc..939a2bc3 100644 --- a/ios/Classes/core/PMManager.m +++ b/ios/Classes/core/PMManager.m @@ -565,7 +565,7 @@ - (void)fetchFullSizeImageFile:(PHAsset *)asset resultHandler:(NSObject