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

fix: add support for iOS missing exif data #2165

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions ios/ImagePickerManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ - (void) showPickerViewController:(UIViewController *)picker


-(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phAsset:(PHAsset * _Nullable)phAsset {
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL);
NSMutableDictionary *imageMetadata = [(NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL)) mutableCopy];

NSString *fileType = [ImagePickerUtils getFileType:data];
if (target == camera) {
if ([self.options[@"saveToPhotos"] boolValue]) {
Expand Down Expand Up @@ -184,6 +187,9 @@ -(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phA

NSString *fileName = [self getImageFileName:fileType];
NSString *path = [[NSTemporaryDirectory() stringByStandardizingPath] stringByAppendingPathComponent:fileName];

data = [self writeMetadataIntoImageData:data metadata:imageMetadata];

[data writeToFile:path atomically:YES];

if ([self.options[@"includeBase64"] boolValue]) {
Expand Down Expand Up @@ -213,6 +219,38 @@ -(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phA
return asset;
}

-(NSData *)writeMetadataIntoImageData:(NSData *)imageData metadata:(NSMutableDictionary *)metadata {
// create an imagesourceref
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData, NULL);

// this is the type of image (e.g., public.jpeg)
CFStringRef UTI = CGImageSourceGetType(source);

// create a new data object and write the new image into it
NSMutableData *dest_data = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data, UTI, 1, NULL);
if (!destination) {
}
// add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef) metadata);
BOOL success = NO;
success = CGImageDestinationFinalize(destination);
if (!success) {
}
if (destination) {
CFRelease(destination);
}
if(source){
CFRelease(source);
}
return [dest_data copy];
}
static dispatch_time_t getDispatchTimeFromSeconds(float seconds) {
long long milliseconds = seconds * 1000.0;
dispatch_time_t waitTime = dispatch_time( DISPATCH_TIME_NOW, 1000000LL * milliseconds );
return waitTime;
}

CGImagePropertyOrientation CGImagePropertyOrientationForUIImageOrientation(UIImageOrientation uiOrientation) {
//code from here: https://developer.apple.com/documentation/imageio/cgimagepropertyorientation?language=objc
switch (uiOrientation) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-image-picker",
"version": "5.6.0",
"version": "5.6.1",
"description": "A React Native module that allows you to use native UI to select media from the device library or directly from the camera",
"react-native": "src/index.ts",
"main": "src/index.ts",
Expand Down