Skip to content

Commit

Permalink
🐛 Reuse file when saving image on Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Oct 1, 2024
1 parent 3f49ed8 commit 915662c
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions ios/Classes/core/PMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1175,14 +1175,10 @@ - (void)saveImage:(NSData *)data

[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^{
PHAssetCreationRequest *request =
[PHAssetCreationRequest creationRequestForAsset];
PHAssetResourceCreationOptions *options =
[PHAssetResourceCreationOptions new];
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
PHAssetResourceCreationOptions *options = [PHAssetResourceCreationOptions new];
[options setOriginalFilename:filename];
[request addResourceWithType:PHAssetResourceTypePhoto
data:data
options:options];
[request addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
assetId = request.placeholderForCreatedAsset.localIdentifier;
}
completionHandler:^(BOOL success, NSError *error) {
Expand All @@ -1200,20 +1196,24 @@ - (void)saveImageWithPath:(NSString *)path
filename:(NSString *)filename
desc:(NSString *)desc
block:(AssetBlockResult)block {
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:path]) {
block(nil, [NSString stringWithFormat:@"File does not exist at %@", path]);
return;
}

[PMLogUtils.sharedInstance info:[NSString stringWithFormat:@"save image with path: %@ filename:%@, desc: %@", path, filename, desc]];

NSURL *fileURL = [NSURL fileURLWithPath:path];
__block NSString *assetId = nil;
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^{
PHAssetCreationRequest *request =
[PHAssetCreationRequest creationRequestForAsset];
PHAssetResourceCreationOptions *options =
[PHAssetResourceCreationOptions new];
[options setOriginalFilename:filename];
NSData *data = [NSData dataWithContentsOfFile:path];
[request addResourceWithType:PHAssetResourceTypePhoto
data:data
options:options];
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
PHAssetResourceCreationOptions *options = [PHAssetResourceCreationOptions new];
if (filename) {
[options setOriginalFilename:filename];
}
[request addResourceWithType:PHAssetResourceTypePhoto fileURL:fileURL options:options];
assetId = request.placeholderForCreatedAsset.localIdentifier;
}
completionHandler:^(BOOL success, NSError *error) {
Expand All @@ -1230,14 +1230,23 @@ - (void)saveVideo:(NSString *)path
filename:(NSString *)filename
desc:(NSString *)desc
block:(AssetBlockResult)block {
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:path]) {
block(nil, [NSString stringWithFormat:@"File does not exist at %@", path]);
return;
}

[PMLogUtils.sharedInstance info:[NSString stringWithFormat:@"save video with path: %@, filename: %@, desc %@", path, filename, desc]];

NSURL *fileURL = [NSURL fileURLWithPath:path];
__block NSString *assetId = nil;
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^{
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
PHAssetResourceCreationOptions *options = [PHAssetResourceCreationOptions new];
[options setOriginalFilename:filename];
if (filename) {
[options setOriginalFilename:filename];
}
[request addResourceWithType:PHAssetResourceTypeVideo fileURL:fileURL options:options];
assetId = request.placeholderForCreatedAsset.localIdentifier;
}
Expand Down

0 comments on commit 915662c

Please sign in to comment.