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

Commit 2706bc6

Browse files
committed
[image_picker] optionally disable PHAsset
1 parent 04cf809 commit 2706bc6

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

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
@@ -40,14 +40,16 @@ void main() {
4040
'maxWidth': null,
4141
'maxHeight': null,
4242
'imageQuality': null,
43-
'cameraDevice': 0
43+
'cameraDevice': 0,
44+
'forceFullMetaData': true,
4445
}),
4546
isMethodCall('pickImage', arguments: <String, dynamic>{
4647
'source': 1,
4748
'maxWidth': null,
4849
'maxHeight': null,
4950
'imageQuality': null,
50-
'cameraDevice': 0
51+
'cameraDevice': 0,
52+
'forceFullMetaData': true,
5153
}),
5254
],
5355
);
@@ -93,49 +95,56 @@ void main() {
9395
'maxWidth': null,
9496
'maxHeight': null,
9597
'imageQuality': null,
96-
'cameraDevice': 0
98+
'cameraDevice': 0,
99+
'forceFullMetaData': true,
97100
}),
98101
isMethodCall('pickImage', arguments: <String, dynamic>{
99102
'source': 0,
100103
'maxWidth': 10.0,
101104
'maxHeight': null,
102105
'imageQuality': null,
103-
'cameraDevice': 0
106+
'cameraDevice': 0,
107+
'forceFullMetaData': true,
104108
}),
105109
isMethodCall('pickImage', arguments: <String, dynamic>{
106110
'source': 0,
107111
'maxWidth': null,
108112
'maxHeight': 10.0,
109113
'imageQuality': null,
110-
'cameraDevice': 0
114+
'cameraDevice': 0,
115+
'forceFullMetaData': true,
111116
}),
112117
isMethodCall('pickImage', arguments: <String, dynamic>{
113118
'source': 0,
114119
'maxWidth': 10.0,
115120
'maxHeight': 20.0,
116121
'imageQuality': null,
117-
'cameraDevice': 0
122+
'cameraDevice': 0,
123+
'forceFullMetaData': true,
118124
}),
119125
isMethodCall('pickImage', arguments: <String, dynamic>{
120126
'source': 0,
121127
'maxWidth': 10.0,
122128
'maxHeight': null,
123129
'imageQuality': 70,
124-
'cameraDevice': 0
130+
'cameraDevice': 0,
131+
'forceFullMetaData': true,
125132
}),
126133
isMethodCall('pickImage', arguments: <String, dynamic>{
127134
'source': 0,
128135
'maxWidth': null,
129136
'maxHeight': 10.0,
130137
'imageQuality': 70,
131-
'cameraDevice': 0
138+
'cameraDevice': 0,
139+
'forceFullMetaData': true,
132140
}),
133141
isMethodCall('pickImage', arguments: <String, dynamic>{
134142
'source': 0,
135143
'maxWidth': 10.0,
136144
'maxHeight': 20.0,
137145
'imageQuality': 70,
138-
'cameraDevice': 0
146+
'cameraDevice': 0,
147+
'forceFullMetaData': true,
139148
}),
140149
],
141150
);
@@ -196,6 +205,7 @@ void main() {
196205
'maxHeight': null,
197206
'imageQuality': null,
198207
'cameraDevice': 0,
208+
'forceFullMetaData': true,
199209
}),
200210
],
201211
);
@@ -215,6 +225,7 @@ void main() {
215225
'maxHeight': null,
216226
'imageQuality': null,
217227
'cameraDevice': 1,
228+
'forceFullMetaData': true,
218229
}),
219230
],
220231
);

0 commit comments

Comments
 (0)