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

Commit c13e8a5

Browse files
[image_picker] add requestFullMetadata for iOS (optional permissions) - platform interface (#5603)
1 parent a15d65d commit c13e8a5

File tree

7 files changed

+409
-26
lines changed

7 files changed

+409
-26
lines changed

packages/image_picker/image_picker_platform_interface/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
## NEXT
1+
## 2.5.0
22

3-
* Minor fixes for new analysis options.
3+
* Deprecates `getImage` in favor of a new method `getImageFromSource`.
4+
* Adds `requestFullMetadata` option that allows disabling extra permission requests
5+
on certain platforms.
6+
* Moves optional image picking parameters to `ImagePickerOptions` class.
7+
* Minor fixes for new analysis options.
48

59
## 2.4.4
610

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
8787
double? maxHeight,
8888
int? imageQuality,
8989
CameraDevice preferredCameraDevice = CameraDevice.rear,
90+
bool requestFullMetadata = true,
9091
}) {
9192
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
9293
throw ArgumentError.value(
@@ -108,7 +109,8 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
108109
'maxWidth': maxWidth,
109110
'maxHeight': maxHeight,
110111
'imageQuality': imageQuality,
111-
'cameraDevice': preferredCameraDevice.index
112+
'cameraDevice': preferredCameraDevice.index,
113+
'requestFullMetadata': requestFullMetadata,
112114
},
113115
);
114116
}
@@ -197,6 +199,22 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
197199
return path != null ? XFile(path) : null;
198200
}
199201

202+
@override
203+
Future<XFile?> getImageFromSource({
204+
required ImageSource source,
205+
ImagePickerOptions options = const ImagePickerOptions(),
206+
}) async {
207+
final String? path = await _getImagePath(
208+
source: source,
209+
maxHeight: options.maxHeight,
210+
maxWidth: options.maxWidth,
211+
imageQuality: options.imageQuality,
212+
preferredCameraDevice: options.preferredCameraDevice,
213+
requestFullMetadata: options.requestFullMetadata,
214+
);
215+
return path != null ? XFile(path) : null;
216+
}
217+
200218
@override
201219
Future<List<XFile>?> getMultiImage({
202220
double? maxWidth,

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ abstract class ImagePickerPlatform extends PlatformInterface {
146146
throw UnimplementedError('retrieveLostData() has not been implemented.');
147147
}
148148

149+
/// This method is deprecated in favor of [getImageFromSource] and will be removed in a future update.
150+
///
149151
/// Returns an [XFile] with the image that was picked.
150152
///
151153
/// The `source` argument controls where the image comes from. This can
@@ -251,4 +253,34 @@ abstract class ImagePickerPlatform extends PlatformInterface {
251253
Future<LostDataResponse> getLostData() {
252254
throw UnimplementedError('getLostData() has not been implemented.');
253255
}
256+
257+
/// Returns an [XFile] with the image that was picked.
258+
///
259+
/// The `source` argument controls where the image comes from. This can
260+
/// be either [ImageSource.camera] or [ImageSource.gallery].
261+
///
262+
/// The `options` argument controls additional settings that can be used when
263+
/// picking an image. See [ImagePickerOptions] for more details.
264+
///
265+
/// Where iOS supports HEIC images, Android 8 and below doesn't. Android 9 and
266+
/// above only support HEIC images if used in addition to a size modification,
267+
/// of which the usage is explained in [ImagePickerOptions].
268+
///
269+
/// In Android, the MainActivity can be destroyed for various reasons. If that
270+
/// happens, the result will be lost in this call. You can then call [getLostData]
271+
/// when your app relaunches to retrieve the lost data.
272+
///
273+
/// If no images were picked, the return value is null.
274+
Future<XFile?> getImageFromSource({
275+
required ImageSource source,
276+
ImagePickerOptions options = const ImagePickerOptions(),
277+
}) {
278+
return getImage(
279+
source: source,
280+
maxHeight: options.maxHeight,
281+
maxWidth: options.maxWidth,
282+
imageQuality: options.imageQuality,
283+
preferredCameraDevice: options.preferredCameraDevice,
284+
);
285+
}
254286
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:image_picker_platform_interface/src/types/types.dart';
6+
7+
/// Specifies options for picking a single image from the device's camera or gallery.
8+
class ImagePickerOptions {
9+
/// Creates an instance with the given [maxHeight], [maxWidth], [imageQuality],
10+
/// [referredCameraDevice] and [requestFullMetadata].
11+
const ImagePickerOptions({
12+
this.maxHeight,
13+
this.maxWidth,
14+
this.imageQuality,
15+
this.preferredCameraDevice = CameraDevice.rear,
16+
this.requestFullMetadata = true,
17+
});
18+
19+
/// The maximum width of the image, in pixels.
20+
///
21+
/// If null, the image will only be resized if [maxHeight] is specified.
22+
final double? maxWidth;
23+
24+
/// The maximum height of the image, in pixels.
25+
///
26+
/// If null, the image will only be resized if [maxWidth] is specified.
27+
final double? maxHeight;
28+
29+
/// Modifies the quality of the image, ranging from 0-100 where 100 is the
30+
/// original/max quality.
31+
///
32+
/// Compression is only supported for certain image types such as JPEG. If
33+
/// compression is not supported for the image that is picked, a warning
34+
/// message will be logged.
35+
///
36+
/// If null, the image will be returned with the original quality.
37+
final int? imageQuality;
38+
39+
/// Used to specify the camera to use when the `source` is [ImageSource.camera].
40+
///
41+
/// Ignored if the source is not [ImageSource.camera], or the chosen camera is not
42+
/// supported on the device. Defaults to [CameraDevice.rear].
43+
final CameraDevice preferredCameraDevice;
44+
45+
/// If true, requests full image metadata, which may require extra permissions
46+
/// on some platforms, (e.g., NSPhotoLibraryUsageDescription on iOS).
47+
//
48+
// Defaults to true.
49+
final bool requestFullMetadata;
50+
}

packages/image_picker/image_picker_platform_interface/lib/src/types/types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
export 'camera_device.dart';
6+
export 'image_picker_options.dart';
67
export 'image_source.dart';
78
export 'lost_data_response.dart';
89
export 'picked_file/picked_file.dart';

packages/image_picker/image_picker_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/image_picker/i
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.4.4
7+
version: 2.5.0
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"

0 commit comments

Comments
 (0)