Skip to content
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.
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,
},
},
];
95 changes: 34 additions & 61 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 @@ -154,35 +145,29 @@ - (void) showPickerViewController:(UIViewController *)picker



-(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phAsset:(PHAsset * _Nullable)phAsset {
NSString *fileType = [ImagePickerUtils getFileType:data];
-(NSMutableDictionary *)mapImageToAsset:(UIImage *)image phAsset:(PHAsset * _Nullable)phAsset {
// NSString *fileType = [ImagePickerUtils getFileType:data];
if (target == camera) {
if ([self.options[@"saveToPhotos"] boolValue]) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
data = extractImageData(image);
// data = extractImageData(image);
}

UIImage* newImage = image;
if (![fileType isEqualToString:@"gif"]) {
newImage = [ImagePickerUtils resizeImage:image
maxWidth:[self.options[@"maxWidth"] floatValue]
maxHeight:[self.options[@"maxHeight"] floatValue]];
}
// if (![fileType isEqualToString:@"gif"]) {
// newImage = [ImagePickerUtils resizeImage:image
// maxWidth:[self.options[@"maxWidth"] floatValue]
// maxHeight:[self.options[@"maxHeight"] floatValue]];
// }

float quality = [self.options[@"quality"] floatValue];
if (![image isEqual:newImage] || (quality >= 0 && quality < 1)) {
if ([fileType isEqualToString:@"jpg"]) {
data = UIImageJPEGRepresentation(newImage, quality);
} else if ([fileType isEqualToString:@"png"]) {
data = UIImagePNGRepresentation(newImage);
}
}

NSData *data = UIImageJPEGRepresentation(newImage, quality);


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 +185,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 +254,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 +386,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 @@ -488,14 +466,9 @@ - (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 All @@ -511,7 +484,7 @@ - (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPick
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];

assets[index] = [self mapImageToAsset:image data:data phAsset:asset];
assets[index] = [self mapImageToAsset:image phAsset:asset];
dispatch_group_leave(completionGroup);
}];
} else if ([provider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeMovie]) {
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