Skip to content

Commit

Permalink
Merge pull request #478 from JimmyTai/fix-video-size-keep-changing
Browse files Browse the repository at this point in the history
fetchFullSizeVideo will cause a loop and return file path before export session process done.
  • Loading branch information
CaiJingLong authored Apr 19, 2021
2 parents 3d72be4 + 8c648b1 commit 277fe35
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 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 Expand Up @@ -563,7 +565,7 @@ - (void)fetchFullSizeImageFile:(PHAsset *)asset resultHandler:(NSObject <PMResul
[options setProgressHandler:^(double progress, NSError *error, BOOL *stop,
NSDictionary *info) {
if (progress == 1.0) {
[self fetchFullSizeImageFile:asset resultHandler:handler progressHandler:nil];
[self notifyProgress:progressHandler progress:progress state:PMProgressStateLoading];
}

if (error) {
Expand All @@ -580,25 +582,23 @@ - (void)fetchFullSizeImageFile:(PHAsset *)asset resultHandler:(NSObject <PMResul
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeDefault
options:options
resultHandler:^(PMImage *_Nullable image,
NSDictionary *_Nullable info) {

BOOL downloadFinished = [PMManager isDownloadFinish:info];
if (!downloadFinished) {
return;
}

if ([handler isReplied]) {
return;
}

NSData *data = [PMImageUtil convertToData:image formatType:PMThumbFormatTypeJPEG quality:1.0];

NSString *path = [self writeFullFileWithAssetId:asset imageData: data];

[handler reply:path];
[self notifySuccess:progressHandler];
}];
resultHandler:^(UIImage *_Nullable image,
NSDictionary *_Nullable info) {
if ([handler isReplied]) {
return;
}

BOOL downloadFinished = [PMManager isDownloadFinish:info];
if (!downloadFinished) {
[handler reply:nil];
return;
}

NSString *path = [self writeFullFileWithAssetId:asset imageData:UIImageJPEGRepresentation(image, 1.0)];

[self notifySuccess:progressHandler];
[handler reply:path];
}];
}

- (NSString *)writeFullFileWithAssetId:(PHAsset *)asset imageData:(NSData *)imageData {
Expand Down Expand Up @@ -666,8 +666,8 @@ - (void)fetchOriginImageFile:(PHAsset *)asset resultHandler:(NSObject <PMResultH
NSLog(@"error = %@", error);
[handler reply:nil];
} else {
[handler reply:path];
[self notifySuccess:progressHandler];
[handler reply:path];
}
}];
}
Expand Down

0 comments on commit 277fe35

Please sign in to comment.