How to Retrieve original file name iOS #2269
Replies: 4 comments 13 replies
-
Hi @parthjdabhi, it didn't work for me, but this other solution works and get the real file name. - (NSString *)getImageFileNameFrom:(PHAsset * _Nullable)phAsset ForType:(NSString *)fileType
{
if (phAsset) {
NSArray<PHAssetResource *> *resources = [PHAssetResource assetResourcesForAsset:phAsset];
if (resources.count > 0) {
NSString *name = resources.firstObject.originalFilename;
// Replace .HEIC extension with .jpg
if ([name hasSuffix:@"HEIC"]) {
name = [name stringByReplacingOccurrencesOfString:@"HEIC" withString:@"jpg"];
}
return name;
}
}
NSString *fileName = [[NSUUID UUID] UUIDString];
fileName = [fileName stringByAppendingString:@"."];
return [fileName stringByAppendingString:fileType];
} Result: {
"asset": {
"fileName": "JPEG sample.jpeg",
"fileSize": 9166,
"height": 183,
"id": "1FA1D528-72ED-450C-88F4-AADFB74B49C0/L0/001",
"timestamp": "2023-04-27T15:00:36.073+0200",
"type": "image/jpg",
"uri": "file:///Users/myUser/Library/Developer/CoreSimulator/Devices/971B3453-A96D-425B-9828-12B27F096F2F/data/Containers/Data/Application/0A19972A-C12D-4C59-8081-1B50C5F3C921/tmp/JPEG%20sample.jpeg",
"width": 275
}
} Now I'd like to do the same for Android... |
Beta Was this translation helpful? Give feedback.
-
Hi @parthjdabhi , can you provide the code changes for android to get the real file name . |
Beta Was this translation helpful? Give feedback.
-
I am not sure if calling this "original filename" is correct. I have no iOS device currently and only worked with iOS some years ago, but isn't it true that the camera only stores heic files since a while? So the original file is always a heic file and its only transcoded to jpg by iOS if the preferredAssetRepresentationMode is set to not set to PHPickerConfigurationAssetRepresentationModeCurrent? This means this picker can't return the correct original filename at all, because nobody want the original name really. That would also explain the needed replacement of the heic extension with jpg that looks strange in all the suggested fixes. |
Beta Was this translation helpful? Give feedback.
-
Hi, I improved upper solutions, because they didn't work for me. Here is my patch file to get the initial filename for iOS: |
Beta Was this translation helpful? Give feedback.
-
There is an issue in react-native-image-picker v5.7.0, That we not getting an original filename instead we gets random UUID-based filename.
Just change this code to start getting the original filename in iOS.
Replace following code in node_modules/react-native-image-picker/ios/ImagePickerManager.mm
With following
and change
[self getImageFileName:fileType];
to[self getImageFileNameFrom:phAsset ForType:fileType];
That's it.
Run an iOS app again and you will start getting an original file name in fileName key.
Beta Was this translation helpful? Give feedback.
All reactions