Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetchFullSizeVideo will cause a loop and return file path before export session process done. #478

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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