Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit e5e720c

Browse files
committed
[image_picker] optionally disable PHAsset
1 parent be327bb commit e5e720c

File tree

7 files changed

+91
-28
lines changed

7 files changed

+91
-28
lines changed

packages/image_picker/image_picker/ios/Classes/FLTImagePickerPlugin.m

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ - (void)pickImageWithPHPicker:(bool)single API_AVAILABLE(ios(14)) {
7878
_pickerViewController = [[PHPickerViewController alloc] initWithConfiguration:config];
7979
_pickerViewController.delegate = self;
8080

81-
[self checkPhotoAuthorizationForAccessLevel];
81+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetaData"] boolValue];
82+
if (usePhaAsset) {
83+
[self checkPhotoAuthorizationForAccessLevel];
84+
return;
85+
}
86+
[self showPhotoLibrary:PHPickerClassType];
8287
}
8388

8489
- (void)pickImageWithUIImagePicker {
@@ -88,6 +93,7 @@ - (void)pickImageWithUIImagePicker {
8893
_imagePickerController.mediaTypes = @[ (NSString *)kUTTypeImage ];
8994

9095
int imageSource = [[_arguments objectForKey:@"source"] intValue];
96+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetaData"] boolValue];
9197

9298
switch (imageSource) {
9399
case SOURCE_CAMERA: {
@@ -98,9 +104,13 @@ - (void)pickImageWithUIImagePicker {
98104
break;
99105
}
100106
case SOURCE_GALLERY:
101-
[self checkPhotoAuthorization];
102-
break;
103-
default:
107+
if (usePhaAsset) {
108+
[self checkPhotoAuthorization];
109+
break;
110+
}
111+
[self showPhotoLibrary:UIImagePickerClassType];
112+
break;
113+
default:
104114
self.result([FlutterError errorWithCode:@"invalid_source"
105115
message:@"Invalid image source."
106116
details:nil]);
@@ -115,14 +125,15 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
115125
details:nil]);
116126
self.result = nil;
117127
}
128+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetaData"] boolValue];
118129

119130
if ([@"pickImage" isEqualToString:call.method]) {
120131
self.result = result;
121132
_arguments = call.arguments;
122133
int imageSource = [[_arguments objectForKey:@"source"] intValue];
123134

124135
if (imageSource == SOURCE_GALLERY) { // Capture is not possible with PHPicker
125-
if (@available(iOS 14, *)) {
136+
if (usePhaAsset && @available(iOS 14, *)) {
126137
// PHPicker is used
127138
[self pickImageWithPHPicker:true];
128139
} else {
@@ -133,7 +144,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
133144
[self pickImageWithUIImagePicker];
134145
}
135146
} else if ([@"pickMultiImage" isEqualToString:call.method]) {
136-
if (@available(iOS 14, *)) {
147+
if (usePhaAsset && @available(iOS 14, *)) {
137148
self.result = result;
138149
_arguments = call.arguments;
139150
[self pickImageWithPHPicker:false];
@@ -152,6 +163,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
152163
_arguments = call.arguments;
153164

154165
int imageSource = [[_arguments objectForKey:@"source"] intValue];
166+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetaData"] boolValue];
155167
if ([[_arguments objectForKey:@"maxDuration"] isKindOfClass:[NSNumber class]]) {
156168
NSTimeInterval max = [[_arguments objectForKey:@"maxDuration"] doubleValue];
157169
_imagePickerController.videoMaximumDuration = max;
@@ -162,7 +174,11 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
162174
[self checkCameraAuthorization];
163175
break;
164176
case SOURCE_GALLERY:
165-
[self checkPhotoAuthorization];
177+
if (usePhaAsset) {
178+
[self checkPhotoAuthorization];
179+
break;
180+
}
181+
[self showPhotoLibrary:UIImagePickerClassType];
166182
break;
167183
default:
168184
result([FlutterError errorWithCode:@"invalid_source"
@@ -357,6 +373,7 @@ - (void)picker:(PHPickerViewController *)picker
357373
NSNumber *maxHeight = [_arguments objectForKey:@"maxHeight"];
358374
NSNumber *imageQuality = [_arguments objectForKey:@"imageQuality"];
359375
NSNumber *desiredImageQuality = [self getDesiredImageQuality:imageQuality];
376+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetaData"] boolValue];
360377

361378
for (PHPickerResult *result in results) {
362379
[result.itemProvider
@@ -373,8 +390,10 @@ - (void)picker:(PHPickerViewController *)picker
373390
maxHeight:maxHeight];
374391
}
375392

376-
PHAsset *originalAsset =
377-
[FLTImagePickerPhotoAssetUtil getAssetFromPHPickerResult:result];
393+
PHAsset *originalAsset;
394+
if (usePhaAsset) {
395+
originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromPHPickerResult:result];
396+
}
378397

379398
if (!originalAsset) {
380399
// Image picked without an original asset (e.g. User took a photo directly)
@@ -448,12 +467,16 @@ - (void)imagePickerController:(UIImagePickerController *)picker
448467
NSNumber *maxHeight = [_arguments objectForKey:@"maxHeight"];
449468
NSNumber *imageQuality = [_arguments objectForKey:@"imageQuality"];
450469
NSNumber *desiredImageQuality = [self getDesiredImageQuality:imageQuality];
470+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetaData"] boolValue];
451471

452472
if (maxWidth != (id)[NSNull null] || maxHeight != (id)[NSNull null]) {
453473
image = [FLTImagePickerImageUtil scaledImage:image maxWidth:maxWidth maxHeight:maxHeight];
454474
}
455475

456-
PHAsset *originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];
476+
PHAsset *originalAsset;
477+
if (usePhaAsset) {
478+
originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];
479+
}
457480
if (!originalAsset) {
458481
// Image picked without an original asset (e.g. User took a photo directly)
459482
[self saveImageWithPickerInfo:info image:image imageQuality:desiredImageQuality];

packages/image_picker/image_picker/lib/image_picker.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class ImagePicker {
4646
/// image types such as JPEG and on Android PNG and WebP, too. If compression is not supported for the image that is picked,
4747
/// a warning message will be logged.
4848
///
49+
/// `forceFullMetaData` defaults to `true`, so the plugin tries to get the full image metadata which may require
50+
/// extra permission requests on certain platforms.
51+
/// If `forceFullMetaData` is set to `false`, the plugin fetches the image in a way that reduces permission requests
52+
/// from the platform (e.g on iOS the plugin won’t ask for the `NSPhotoLibraryUsageDescription` permission).
53+
///
4954
/// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
5055
/// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
5156
/// Defaults to [CameraDevice.rear]. Note that Android has no documented parameter for an intent to specify if
@@ -59,13 +64,15 @@ class ImagePicker {
5964
double? maxWidth,
6065
double? maxHeight,
6166
int? imageQuality,
67+
bool forceFullMetaData = true,
6268
CameraDevice preferredCameraDevice = CameraDevice.rear,
6369
}) {
6470
return platform.pickImage(
6571
source: source,
6672
maxWidth: maxWidth,
6773
maxHeight: maxHeight,
6874
imageQuality: imageQuality,
75+
forceFullMetaData: forceFullMetaData,
6976
preferredCameraDevice: preferredCameraDevice,
7077
);
7178
}

packages/image_picker/image_picker/test/image_picker_test.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ void main() {
5151
'maxWidth': null,
5252
'maxHeight': null,
5353
'imageQuality': null,
54-
'cameraDevice': 0
54+
'cameraDevice': 0,
55+
'forceFullMetaData': true,
5556
}),
5657
isMethodCall('pickImage', arguments: <String, dynamic>{
5758
'source': 1,
5859
'maxWidth': null,
5960
'maxHeight': null,
6061
'imageQuality': null,
61-
'cameraDevice': 0
62+
'cameraDevice': 0,
63+
'forceFullMetaData': true,
6264
}),
6365
],
6466
);
@@ -97,49 +99,56 @@ void main() {
9799
'maxWidth': null,
98100
'maxHeight': null,
99101
'imageQuality': null,
100-
'cameraDevice': 0
102+
'cameraDevice': 0,
103+
'forceFullMetaData': true,
101104
}),
102105
isMethodCall('pickImage', arguments: <String, dynamic>{
103106
'source': 0,
104107
'maxWidth': 10.0,
105108
'maxHeight': null,
106109
'imageQuality': null,
107-
'cameraDevice': 0
110+
'cameraDevice': 0,
111+
'forceFullMetaData': true,
108112
}),
109113
isMethodCall('pickImage', arguments: <String, dynamic>{
110114
'source': 0,
111115
'maxWidth': null,
112116
'maxHeight': 10.0,
113117
'imageQuality': null,
114-
'cameraDevice': 0
118+
'cameraDevice': 0,
119+
'forceFullMetaData': true,
115120
}),
116121
isMethodCall('pickImage', arguments: <String, dynamic>{
117122
'source': 0,
118123
'maxWidth': 10.0,
119124
'maxHeight': 20.0,
120125
'imageQuality': null,
121-
'cameraDevice': 0
126+
'cameraDevice': 0,
127+
'forceFullMetaData': true,
122128
}),
123129
isMethodCall('pickImage', arguments: <String, dynamic>{
124130
'source': 0,
125131
'maxWidth': 10.0,
126132
'maxHeight': null,
127133
'imageQuality': 70,
128-
'cameraDevice': 0
134+
'cameraDevice': 0,
135+
'forceFullMetaData': true,
129136
}),
130137
isMethodCall('pickImage', arguments: <String, dynamic>{
131138
'source': 0,
132139
'maxWidth': null,
133140
'maxHeight': 10.0,
134141
'imageQuality': 70,
135-
'cameraDevice': 0
142+
'cameraDevice': 0,
143+
'forceFullMetaData': true,
136144
}),
137145
isMethodCall('pickImage', arguments: <String, dynamic>{
138146
'source': 0,
139147
'maxWidth': 10.0,
140148
'maxHeight': 20.0,
141149
'imageQuality': 70,
142-
'cameraDevice': 0
150+
'cameraDevice': 0,
151+
'forceFullMetaData': true,
143152
}),
144153
],
145154
);
@@ -176,6 +185,7 @@ void main() {
176185
'maxHeight': null,
177186
'imageQuality': null,
178187
'cameraDevice': 0,
188+
'forceFullMetaData': true,
179189
}),
180190
],
181191
);
@@ -195,6 +205,7 @@ void main() {
195205
'maxHeight': null,
196206
'imageQuality': null,
197207
'cameraDevice': 1,
208+
'forceFullMetaData': true,
198209
}),
199210
],
200211
);

packages/image_picker/image_picker_for_web/lib/image_picker_for_web.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ImagePickerPlugin extends ImagePickerPlatform {
5252
double? maxWidth,
5353
double? maxHeight,
5454
int? imageQuality,
55+
bool forceFullMetaData = true,
5556
CameraDevice preferredCameraDevice = CameraDevice.rear,
5657
}) {
5758
String? capture = computeCaptureAttribute(source, preferredCameraDevice);

packages/image_picker/image_picker_platform_interface/lib/src/method_channel/method_channel_image_picker.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
2424
double? maxWidth,
2525
double? maxHeight,
2626
int? imageQuality,
27+
bool forceFullMetaData = true,
2728
CameraDevice preferredCameraDevice = CameraDevice.rear,
2829
}) async {
2930
String? path = await _pickImagePath(
3031
source: source,
3132
maxWidth: maxWidth,
3233
maxHeight: maxHeight,
3334
imageQuality: imageQuality,
35+
forceFullMetaData: forceFullMetaData,
3436
preferredCameraDevice: preferredCameraDevice,
3537
);
3638
return path != null ? PickedFile(path) : null;
@@ -89,6 +91,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
8991
double? maxWidth,
9092
double? maxHeight,
9193
int? imageQuality,
94+
bool forceFullMetaData = true,
9295
CameraDevice preferredCameraDevice = CameraDevice.rear,
9396
}) {
9497
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
@@ -111,6 +114,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
111114
'maxWidth': maxWidth,
112115
'maxHeight': maxHeight,
113116
'imageQuality': imageQuality,
117+
'forceFullMetaData': forceFullMetaData,
114118
'cameraDevice': preferredCameraDevice.index
115119
},
116120
);

packages/image_picker/image_picker_platform_interface/lib/src/platform_interface/image_picker_platform.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ abstract class ImagePickerPlatform extends PlatformInterface {
5858
/// image types such as JPEG. If compression is not supported for the image that is picked,
5959
/// a warning message will be logged.
6060
///
61+
/// `forceFullMetaData` defaults to `true`, so the plugin tries to get the full image metadata which may require
62+
/// extra permission requests on certain platforms.
63+
/// If `forceFullMetaData` is set to `false`, the plugin fetches the image in a way that reduces permission requests
64+
/// from the platform (e.g on iOS the plugin won’t ask for the `NSPhotoLibraryUsageDescription` permission).
65+
///
6166
/// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
6267
/// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
6368
/// Defaults to [CameraDevice.rear]. Note that Android has no documented parameter for an intent to specify if
@@ -73,6 +78,7 @@ abstract class ImagePickerPlatform extends PlatformInterface {
7378
double? maxWidth,
7479
double? maxHeight,
7580
int? imageQuality,
81+
bool forceFullMetaData = true,
7682
CameraDevice preferredCameraDevice = CameraDevice.rear,
7783
}) {
7884
throw UnimplementedError('pickImage() has not been implemented.');

0 commit comments

Comments
 (0)