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

Commit a49b7eb

Browse files
committed
[image_picker] optionally disable PHAsset
1 parent 7d49fd4 commit a49b7eb

File tree

7 files changed

+84
-24
lines changed

7 files changed

+84
-24
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ - (void)pickImageWithPHPicker:(int)maxImagesAllowed API_AVAILABLE(ios(14)) {
9595

9696
self.maxImagesAllowed = maxImagesAllowed;
9797

98-
[self checkPhotoAuthorizationForAccessLevel];
98+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetadata"] boolValue];
99+
if (usePhaAsset) {
100+
[self checkPhotoAuthorizationForAccessLevel];
101+
return;
102+
}
103+
[self showPhotoLibrary:PHPickerClassType];
99104
}
100105

101106
- (void)pickImageWithUIImagePicker {
@@ -105,6 +110,7 @@ - (void)pickImageWithUIImagePicker {
105110
_imagePickerController.mediaTypes = @[ (NSString *)kUTTypeImage ];
106111

107112
int imageSource = [[_arguments objectForKey:@"source"] intValue];
113+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetadata"] boolValue];
108114

109115
self.maxImagesAllowed = 1;
110116

@@ -113,8 +119,12 @@ - (void)pickImageWithUIImagePicker {
113119
[self checkCameraAuthorization];
114120
break;
115121
case SOURCE_GALLERY:
116-
[self checkPhotoAuthorization];
117-
break;
122+
if (usePhaAsset) {
123+
[self checkPhotoAuthorization];
124+
break;
125+
}
126+
[self showPhotoLibrary:UIImagePickerClassType];
127+
break;
118128
default:
119129
self.result([FlutterError errorWithCode:@"invalid_source"
120130
message:@"Invalid image source."
@@ -130,13 +140,14 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
130140
details:nil]);
131141
self.result = nil;
132142
}
143+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetadata"] boolValue];
133144

134145
if ([@"pickImage" isEqualToString:call.method]) {
135146
self.result = result;
136147
_arguments = call.arguments;
137148
int imageSource = [[_arguments objectForKey:@"source"] intValue];
138149

139-
if (imageSource == SOURCE_GALLERY) { // Capture is not possible with PHPicker
150+
if (usePhaAsset && imageSource == SOURCE_GALLERY) { // Capture is not possible with PHPicker
140151
if (@available(iOS 14, *)) {
141152
// PHPicker is used
142153
[self pickImageWithPHPicker:1];
@@ -169,6 +180,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
169180
_arguments = call.arguments;
170181

171182
int imageSource = [[_arguments objectForKey:@"source"] intValue];
183+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetadata"] boolValue];
172184
if ([[_arguments objectForKey:@"maxDuration"] isKindOfClass:[NSNumber class]]) {
173185
NSTimeInterval max = [[_arguments objectForKey:@"maxDuration"] doubleValue];
174186
_imagePickerController.videoMaximumDuration = max;
@@ -179,7 +191,11 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
179191
[self checkCameraAuthorization];
180192
break;
181193
case SOURCE_GALLERY:
182-
[self checkPhotoAuthorization];
194+
if (usePhaAsset) {
195+
[self checkPhotoAuthorization];
196+
break;
197+
}
198+
[self showPhotoLibrary:UIImagePickerClassType];
183199
break;
184200
default:
185201
result([FlutterError errorWithCode:@"invalid_source"
@@ -472,8 +488,12 @@ - (void)imagePickerController:(UIImagePickerController *)picker
472488
NSNumber *maxHeight = [_arguments objectForKey:@"maxHeight"];
473489
NSNumber *imageQuality = [_arguments objectForKey:@"imageQuality"];
474490
NSNumber *desiredImageQuality = [self getDesiredImageQuality:imageQuality];
491+
BOOL usePhaAsset = [[_arguments objectForKey:@"forceFullMetadata"] boolValue];
475492

476-
PHAsset *originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];
493+
PHAsset *originalAsset;
494+
if (usePhaAsset) {
495+
originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];
496+
}
477497

478498
if (maxWidth != (id)[NSNull null] || maxHeight != (id)[NSNull null]) {
479499
image = [FLTImagePickerImageUtil scaledImage:image

packages/image_picker/image_picker/lib/image_picker.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class ImagePicker {
4444
/// image types such as JPEG and on Android PNG and WebP, too. If compression is not supported for the image that is picked,
4545
/// a warning message will be logged.
4646
///
47+
/// `forceFullMetadata` defaults to `true`, so the plugin tries to get the full image metadata which may require
48+
/// extra permission requests on certain platforms.
49+
/// If `forceFullMetadata` is set to `false`, the plugin fetches the image in a way that reduces permission requests
50+
/// from the platform (e.g on iOS the plugin won’t ask for the `NSPhotoLibraryUsageDescription` permission).
51+
///
4752
/// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
4853
/// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
4954
/// Defaults to [CameraDevice.rear]. Note that Android has no documented parameter for an intent to specify if
@@ -65,13 +70,15 @@ class ImagePicker {
6570
double? maxWidth,
6671
double? maxHeight,
6772
int? imageQuality,
73+
bool forceFullMetadata = true,
6874
CameraDevice preferredCameraDevice = CameraDevice.rear,
6975
}) {
7076
return platform.pickImage(
7177
source: source,
7278
maxWidth: maxWidth,
7379
maxHeight: maxHeight,
7480
imageQuality: imageQuality,
81+
forceFullMetadata: forceFullMetadata,
7582
preferredCameraDevice: preferredCameraDevice,
7683
);
7784
}

packages/image_picker/image_picker/test/image_picker_test.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ void main() {
5252
'maxWidth': null,
5353
'maxHeight': null,
5454
'imageQuality': null,
55-
'cameraDevice': 0
55+
'cameraDevice': 0,
56+
'forceFullMetadata': true,
5657
}),
5758
isMethodCall('pickImage', arguments: <String, dynamic>{
5859
'source': 1,
5960
'maxWidth': null,
6061
'maxHeight': null,
6162
'imageQuality': null,
62-
'cameraDevice': 0
63+
'cameraDevice': 0,
64+
'forceFullMetadata': true,
6365
}),
6466
],
6567
);
@@ -98,49 +100,56 @@ void main() {
98100
'maxWidth': null,
99101
'maxHeight': null,
100102
'imageQuality': null,
101-
'cameraDevice': 0
103+
'cameraDevice': 0,
104+
'forceFullMetadata': true,
102105
}),
103106
isMethodCall('pickImage', arguments: <String, dynamic>{
104107
'source': 0,
105108
'maxWidth': 10.0,
106109
'maxHeight': null,
107110
'imageQuality': null,
108-
'cameraDevice': 0
111+
'cameraDevice': 0,
112+
'forceFullMetadata': true,
109113
}),
110114
isMethodCall('pickImage', arguments: <String, dynamic>{
111115
'source': 0,
112116
'maxWidth': null,
113117
'maxHeight': 10.0,
114118
'imageQuality': null,
115-
'cameraDevice': 0
119+
'cameraDevice': 0,
120+
'forceFullMetadata': true,
116121
}),
117122
isMethodCall('pickImage', arguments: <String, dynamic>{
118123
'source': 0,
119124
'maxWidth': 10.0,
120125
'maxHeight': 20.0,
121126
'imageQuality': null,
122-
'cameraDevice': 0
127+
'cameraDevice': 0,
128+
'forceFullMetadata': true,
123129
}),
124130
isMethodCall('pickImage', arguments: <String, dynamic>{
125131
'source': 0,
126132
'maxWidth': 10.0,
127133
'maxHeight': null,
128134
'imageQuality': 70,
129-
'cameraDevice': 0
135+
'cameraDevice': 0,
136+
'forceFullMetadata': true,
130137
}),
131138
isMethodCall('pickImage', arguments: <String, dynamic>{
132139
'source': 0,
133140
'maxWidth': null,
134141
'maxHeight': 10.0,
135142
'imageQuality': 70,
136-
'cameraDevice': 0
143+
'cameraDevice': 0,
144+
'forceFullMetadata': true,
137145
}),
138146
isMethodCall('pickImage', arguments: <String, dynamic>{
139147
'source': 0,
140148
'maxWidth': 10.0,
141149
'maxHeight': 20.0,
142150
'imageQuality': 70,
143-
'cameraDevice': 0
151+
'cameraDevice': 0,
152+
'forceFullMetadata': true,
144153
}),
145154
],
146155
);
@@ -177,6 +186,7 @@ void main() {
177186
'maxHeight': null,
178187
'imageQuality': null,
179188
'cameraDevice': 0,
189+
'forceFullMetadata': true,
180190
}),
181191
],
182192
);
@@ -196,6 +206,7 @@ void main() {
196206
'maxHeight': null,
197207
'imageQuality': null,
198208
'cameraDevice': 1,
209+
'forceFullMetadata': true,
199210
}),
200211
],
201212
);

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
@@ -53,6 +53,7 @@ class ImagePickerPlugin extends ImagePickerPlatform {
5353
double? maxWidth,
5454
double? maxHeight,
5555
int? imageQuality,
56+
bool forceFullMetadata = true,
5657
CameraDevice preferredCameraDevice = CameraDevice.rear,
5758
}) {
5859
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 _getImagePath(
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;
@@ -85,6 +87,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
8587
double? maxWidth,
8688
double? maxHeight,
8789
int? imageQuality,
90+
bool forceFullMetadata = true,
8891
CameraDevice preferredCameraDevice = CameraDevice.rear,
8992
}) {
9093
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
@@ -107,6 +110,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
107110
'maxWidth': maxWidth,
108111
'maxHeight': maxHeight,
109112
'imageQuality': imageQuality,
113+
'forceFullMetadata': forceFullMetadata,
110114
'cameraDevice': preferredCameraDevice.index
111115
},
112116
);

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)