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

Photo picking issues #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 0 additions & 2 deletions android/src/main/java/com/imagepicker/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public class Options {
int selectionLimit;
Boolean includeBase64;
Boolean includeExtra;
int videoQuality = 1;
int quality;
int maxWidth;
Expand All @@ -21,7 +20,6 @@ public class Options {
mediaType = options.getString("mediaType");
selectionLimit = options.getInt("selectionLimit");
includeBase64 = options.getBoolean("includeBase64");
includeExtra = options.getBoolean("includeExtra");

String videoQualityString = options.getString("videoQuality");
if(!TextUtils.isEmpty(videoQualityString) && !videoQualityString.toLowerCase().equals("high")) {
Expand Down
17 changes: 5 additions & 12 deletions android/src/main/java/com/imagepicker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,17 +404,13 @@ static ReadableMap getImageResponseMap(Uri uri, Options options, Context context
map.putInt("width", dimensions[0]);
map.putInt("height", dimensions[1]);
map.putString("type", getMimeType(uri, context));
map.putString("timestamp", imageMetadata.getDateTime());
map.putString("id", fileName);

if (options.includeBase64) {
map.putString("base64", getBase64String(uri, context));
}

if(options.includeExtra) {
// Add more extra data here ...
map.putString("timestamp", imageMetadata.getDateTime());
map.putString("id", fileName);
}

return map;
}

Expand All @@ -431,12 +427,9 @@ static ReadableMap getVideoResponseMap(Uri uri, Options options, Context context
map.putString("type", getMimeType(uri, context));
map.putInt("width", videoMetadata.getWidth());
map.putInt("height", videoMetadata.getHeight());

if(options.includeExtra) {
// Add more extra data here ...
map.putString("timestamp", videoMetadata.getDateTime());
map.putString("id", fileName);
}
map.putString("timestamp", videoMetadata.getDateTime());
map.putString("id", fileName);
// Add more extra data here ...

return map;
}
Expand Down
8 changes: 0 additions & 8 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import {DemoTitle, DemoButton, DemoResponse} from './components';

import * as ImagePicker from 'react-native-image-picker';

/* toggle includeExtra */
const includeExtra = true;

export default function App() {
const [response, setResponse] = React.useState<any>(null);

Expand Down Expand Up @@ -85,7 +82,6 @@ const actions: Action[] = [
saveToPhotos: true,
mediaType: 'photo',
includeBase64: false,
includeExtra,
},
},
{
Expand All @@ -95,7 +91,6 @@ const actions: Action[] = [
selectionLimit: 0,
mediaType: 'photo',
includeBase64: false,
includeExtra,
},
},
{
Expand All @@ -104,7 +99,6 @@ const actions: Action[] = [
options: {
saveToPhotos: true,
mediaType: 'video',
includeExtra,
},
},
{
Expand All @@ -113,7 +107,6 @@ const actions: Action[] = [
options: {
selectionLimit: 0,
mediaType: 'video',
includeExtra,
},
},
{
Expand All @@ -122,7 +115,6 @@ const actions: Action[] = [
options: {
selectionLimit: 0,
mediaType: 'mixed',
includeExtra,
},
},
];
66 changes: 22 additions & 44 deletions ios/ImagePickerManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,14 @@ - (void)launchImagePicker:(NSDictionary *)options callback:(RCTResponseSenderBlo
picker.delegate = self;
picker.modalPresentationStyle = [RCTConvert UIModalPresentationStyle:options[@"presentationStyle"]];
picker.presentationController.delegate = self;

if([self.options[@"includeExtra"] boolValue]) {

[self checkPhotosPermissions:^(BOOL granted) {
if (!granted) {
self.callback(@[@{@"errorCode": errPermission}]);
return;
}
[self showPickerViewController:picker];
}];
} else {
[self checkPhotosPermissions:^(BOOL granted) {
if (!granted) {
self.callback(@[@{@"errorCode": errPermission}]);
return;
}
[self showPickerViewController:picker];
}
}];

return;
}
Expand All @@ -105,17 +100,13 @@ - (void)launchImagePicker:(NSDictionary *)options callback:(RCTResponseSenderBlo
[ImagePickerUtils setupPickerFromOptions:picker options:self.options target:target];
picker.delegate = self;

if([self.options[@"includeExtra"] boolValue]) {
[self checkPhotosPermissions:^(BOOL granted) {
if (!granted) {
self.callback(@[@{@"errorCode": errPermission}]);
return;
}
[self showPickerViewController:picker];
}];
} else {
[self showPickerViewController:picker];
}
[self checkPhotosPermissions:^(BOOL granted) {
if (!granted) {
self.callback(@[@{@"errorCode": errPermission}]);
return;
}
[self showPickerViewController:picker];
}];
}

- (void) showPickerViewController:(UIViewController *)picker
Expand Down Expand Up @@ -180,9 +171,7 @@ -(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phA
}

NSMutableDictionary *asset = [[NSMutableDictionary alloc] init];
asset[@"type"] = [@"image/" stringByAppendingString:fileType];

NSString *fileName = [self getImageFileName:fileType];
NSString *fileName = [self getImageFileName:@"jpeg"];
NSString *path = [[NSTemporaryDirectory() stringByStandardizingPath] stringByAppendingPathComponent:fileName];
[data writeToFile:path atomically:YES];

Expand All @@ -200,6 +189,7 @@ -(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phA
asset[@"fileSize"] = fileSizeValue;
}

asset[@"type"] = [@"image/" stringByAppendingString:@"jpeg"];
asset[@"fileName"] = fileName;
asset[@"width"] = @(newImage.size.width);
asset[@"height"] = @(newImage.size.height);
Expand Down Expand Up @@ -268,12 +258,9 @@ -(NSMutableDictionary *)mapVideoToAsset:(NSURL *)url phAsset:(PHAsset * _Nullabl
asset[@"fileSize"] = [ImagePickerUtils getFileSizeFromUrl:videoDestinationURL];
asset[@"width"] = @(dimentions.width);
asset[@"height"] = @(dimentions.height);

if(phAsset){
asset[@"timestamp"] = [self getDateTimeInUTC:phAsset.creationDate];
asset[@"id"] = phAsset.localIdentifier;
// Add more extra data here ...
}
asset[@"timestamp"] = [self getDateTimeInUTC:phAsset.creationDate];
asset[@"id"] = phAsset.localIdentifier;
// Add more extra data here ...

return asset;
}
Expand Down Expand Up @@ -403,22 +390,17 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking
{
dispatch_block_t dismissCompletionBlock = ^{
NSMutableArray<NSDictionary *> *assets = [[NSMutableArray alloc] initWithCapacity:1];
PHAsset *asset = nil;
PHAsset *asset = [ImagePickerUtils fetchPHAssetOnIOS13:info];

if (photoSelected == YES) {
return;
}
photoSelected = YES;

// If include extra, we fetch the PHAsset, this required library permissions
if([self.options[@"includeExtra"] boolValue]) {
asset = [ImagePickerUtils fetchPHAssetOnIOS13:info];
}

if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *) kUTTypeImage]) {
UIImage *image = [ImagePickerManager getUIImageFromInfo:info];

[assets addObject:[self mapImageToAsset:image data:[NSData dataWithContentsOfURL:[ImagePickerManager getNSURLFromInfo:info]] phAsset:asset]];
[assets addObject:[self mapImageToAsset:image phAsset:asset]];
} else {
NSError *error;
NSDictionary *videoAsset = [self mapVideoToAsset:info[UIImagePickerControllerMediaURL] phAsset:asset error:&error];
Expand Down Expand Up @@ -490,12 +472,8 @@ - (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPick
[results enumerateObjectsUsingBlock:^(PHPickerResult *result, NSUInteger index, BOOL *stop) {
PHAsset *asset = nil;
NSItemProvider *provider = result.itemProvider;

// If include extra, we fetch the PHAsset, this required library permissions
if([self.options[@"includeExtra"] boolValue] && result.assetIdentifier != nil) {
PHFetchResult* fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[result.assetIdentifier] options:nil];
asset = fetchResult.firstObject;
}
PHFetchResult* fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[result.assetIdentifier] options:nil];
PHAsset *asset = fetchResult.firstObject;

dispatch_group_enter(completionGroup);

Expand Down
8 changes: 2 additions & 6 deletions ios/ImagePickerUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ + (PHPickerConfiguration *)makeConfigurationFromOptions:(NSDictionary *)options
#if __has_include(<PhotosUI/PHPicker.h>)
PHPickerConfiguration *configuration;

if(options[@"includeExtra"]) {
PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
configuration = [[PHPickerConfiguration alloc] initWithPhotoLibrary:photoLibrary];
} else {
configuration = [[PHPickerConfiguration alloc] init];
}
PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
configuration = [[PHPickerConfiguration alloc] initWithPhotoLibrary:photoLibrary];

configuration.preferredAssetRepresentationMode = PHPickerConfigurationAssetRepresentationModeAutomatic;
configuration.selectionLimit = [options[@"selectionLimit"] integerValue];
Expand Down
1 change: 0 additions & 1 deletion src/platforms/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const DEFAULT_OPTIONS: ImageLibraryOptions & CameraOptions = {
selectionLimit: 1,
saveToPhotos: false,
durationLimit: 0,
includeExtra: false,
presentationStyle: 'pageSheet',
};

Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface OptionsCommon {
quality?: PhotoQuality;
videoQuality?: AndroidVideoOptions | iOSVideoOptions;
includeBase64?: boolean;
includeExtra?: boolean;
presentationStyle?:
| 'currentContext'
| 'fullScreen'
Expand Down