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

Commit 33176ce

Browse files
committed
[image_picker] optionally disable PHAsset
1 parent 5787b35 commit 33176ce

File tree

7 files changed

+75
-21
lines changed

7 files changed

+75
-21
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
7676
_arguments = call.arguments;
7777

7878
int imageSource = [[_arguments objectForKey:@"source"] intValue];
79+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetaData"] boolValue];
7980

8081
switch (imageSource) {
8182
case SOURCE_CAMERA: {
@@ -86,7 +87,11 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
8687
break;
8788
}
8889
case SOURCE_GALLERY:
89-
[self checkPhotoAuthorization];
90+
if (usePhaAsset) {
91+
[self checkPhotoAuthorization];
92+
break;
93+
}
94+
[self showPhotoLibrary];
9095
break;
9196
default:
9297
result([FlutterError errorWithCode:@"invalid_source"
@@ -108,6 +113,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
108113
_arguments = call.arguments;
109114

110115
int imageSource = [[_arguments objectForKey:@"source"] intValue];
116+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetaData"] boolValue];
111117
if ([[_arguments objectForKey:@"maxDuration"] isKindOfClass:[NSNumber class]]) {
112118
NSTimeInterval max = [[_arguments objectForKey:@"maxDuration"] doubleValue];
113119
_imagePickerController.videoMaximumDuration = max;
@@ -118,7 +124,11 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
118124
[self checkCameraAuthorization];
119125
break;
120126
case SOURCE_GALLERY:
121-
[self checkPhotoAuthorization];
127+
if (usePhaAsset) {
128+
[self checkPhotoAuthorization];
129+
break;
130+
}
131+
[self showPhotoLibrary];
122132
break;
123133
default:
124134
result([FlutterError errorWithCode:@"invalid_source"
@@ -299,6 +309,7 @@ - (void)imagePickerController:(UIImagePickerController *)picker
299309
NSNumber *maxWidth = [_arguments objectForKey:@"maxWidth"];
300310
NSNumber *maxHeight = [_arguments objectForKey:@"maxHeight"];
301311
NSNumber *imageQuality = [_arguments objectForKey:@"imageQuality"];
312+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetaData"] boolValue];
302313

303314
if (![imageQuality isKindOfClass:[NSNumber class]]) {
304315
imageQuality = @1;
@@ -312,7 +323,10 @@ - (void)imagePickerController:(UIImagePickerController *)picker
312323
image = [FLTImagePickerImageUtil scaledImage:image maxWidth:maxWidth maxHeight:maxHeight];
313324
}
314325

315-
PHAsset *originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];
326+
PHAsset *originalAsset;
327+
if (usePhaAsset) {
328+
originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];
329+
}
316330
if (!originalAsset) {
317331
// Image picked without an original asset (e.g. User took a photo directly)
318332
[self saveImageWithPickerInfo:info image:image imageQuality:imageQuality];

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;
@@ -41,6 +43,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
4143
double? maxWidth,
4244
double? maxHeight,
4345
int? imageQuality,
46+
bool forceFullMetaData = true,
4447
CameraDevice preferredCameraDevice = CameraDevice.rear,
4548
}) {
4649
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
@@ -63,6 +66,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
6366
'maxWidth': maxWidth,
6467
'maxHeight': maxHeight,
6568
'imageQuality': imageQuality,
69+
'forceFullMetaData': forceFullMetaData,
6670
'cameraDevice': preferredCameraDevice.index
6771
},
6872
);

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
/// an 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.');

packages/image_picker/image_picker_platform_interface/test/new_method_channel_image_picker_test.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ void main() {
3838
'maxWidth': null,
3939
'maxHeight': null,
4040
'imageQuality': null,
41-
'cameraDevice': 0
41+
'cameraDevice': 0,
42+
'forceFullMetaData': true,
4243
}),
4344
isMethodCall('pickImage', arguments: <String, dynamic>{
4445
'source': 1,
4546
'maxWidth': null,
4647
'maxHeight': null,
4748
'imageQuality': null,
48-
'cameraDevice': 0
49+
'cameraDevice': 0,
50+
'forceFullMetaData': true,
4951
}),
5052
],
5153
);
@@ -91,49 +93,56 @@ void main() {
9193
'maxWidth': null,
9294
'maxHeight': null,
9395
'imageQuality': null,
94-
'cameraDevice': 0
96+
'cameraDevice': 0,
97+
'forceFullMetaData': true,
9598
}),
9699
isMethodCall('pickImage', arguments: <String, dynamic>{
97100
'source': 0,
98101
'maxWidth': 10.0,
99102
'maxHeight': null,
100103
'imageQuality': null,
101-
'cameraDevice': 0
104+
'cameraDevice': 0,
105+
'forceFullMetaData': true,
102106
}),
103107
isMethodCall('pickImage', arguments: <String, dynamic>{
104108
'source': 0,
105109
'maxWidth': null,
106110
'maxHeight': 10.0,
107111
'imageQuality': null,
108-
'cameraDevice': 0
112+
'cameraDevice': 0,
113+
'forceFullMetaData': true,
109114
}),
110115
isMethodCall('pickImage', arguments: <String, dynamic>{
111116
'source': 0,
112117
'maxWidth': 10.0,
113118
'maxHeight': 20.0,
114119
'imageQuality': null,
115-
'cameraDevice': 0
120+
'cameraDevice': 0,
121+
'forceFullMetaData': true,
116122
}),
117123
isMethodCall('pickImage', arguments: <String, dynamic>{
118124
'source': 0,
119125
'maxWidth': 10.0,
120126
'maxHeight': null,
121127
'imageQuality': 70,
122-
'cameraDevice': 0
128+
'cameraDevice': 0,
129+
'forceFullMetaData': true,
123130
}),
124131
isMethodCall('pickImage', arguments: <String, dynamic>{
125132
'source': 0,
126133
'maxWidth': null,
127134
'maxHeight': 10.0,
128135
'imageQuality': 70,
129-
'cameraDevice': 0
136+
'cameraDevice': 0,
137+
'forceFullMetaData': true,
130138
}),
131139
isMethodCall('pickImage', arguments: <String, dynamic>{
132140
'source': 0,
133141
'maxWidth': 10.0,
134142
'maxHeight': 20.0,
135143
'imageQuality': 70,
136-
'cameraDevice': 0
144+
'cameraDevice': 0,
145+
'forceFullMetaData': true,
137146
}),
138147
],
139148
);
@@ -171,6 +180,7 @@ void main() {
171180
'maxHeight': null,
172181
'imageQuality': null,
173182
'cameraDevice': 0,
183+
'forceFullMetaData': true,
174184
}),
175185
],
176186
);
@@ -190,6 +200,7 @@ void main() {
190200
'maxHeight': null,
191201
'imageQuality': null,
192202
'cameraDevice': 1,
203+
'forceFullMetaData': true,
193204
}),
194205
],
195206
);

0 commit comments

Comments
 (0)