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

[image_picker] optionally disable PHAsset & iOS permission request #3090

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
_arguments = call.arguments;

int imageSource = [[_arguments objectForKey:@"source"] intValue];
BOOL usePhaAsset = [[_arguments objectForKey:@"iosPhaAsset"] boolValue];

switch (imageSource) {
case SOURCE_CAMERA: {
Expand All @@ -86,7 +87,11 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
break;
}
case SOURCE_GALLERY:
[self checkPhotoAuthorization];
if (usePhaAsset) {
[self checkPhotoAuthorization];
break;
}
[self showPhotoLibrary];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may actually need a check for iOS 11 or higher

break;
default:
result([FlutterError errorWithCode:@"invalid_source"
Expand All @@ -108,6 +113,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
_arguments = call.arguments;

int imageSource = [[_arguments objectForKey:@"source"] intValue];
BOOL usePhaAsset = [[_arguments objectForKey:@"iosPhaAsset"] boolValue];
if ([[_arguments objectForKey:@"maxDuration"] isKindOfClass:[NSNumber class]]) {
NSTimeInterval max = [[_arguments objectForKey:@"maxDuration"] doubleValue];
_imagePickerController.videoMaximumDuration = max;
Expand All @@ -118,7 +124,11 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
[self checkCameraAuthorization];
break;
case SOURCE_GALLERY:
[self checkPhotoAuthorization];
if (usePhaAsset) {
[self checkPhotoAuthorization];
break;
}
[self showPhotoLibrary];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, this may need a check for iOS 11 or higher

break;
default:
result([FlutterError errorWithCode:@"invalid_source"
Expand Down Expand Up @@ -299,6 +309,7 @@ - (void)imagePickerController:(UIImagePickerController *)picker
NSNumber *maxWidth = [_arguments objectForKey:@"maxWidth"];
NSNumber *maxHeight = [_arguments objectForKey:@"maxHeight"];
NSNumber *imageQuality = [_arguments objectForKey:@"imageQuality"];
BOOL usePhaAsset = [[_arguments objectForKey:@"iosPhaAsset"] boolValue];

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

PHAsset *originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];
PHAsset *originalAsset;
if (usePhaAsset) {
originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];
}
if (!originalAsset) {
// Image picked without an original asset (e.g. User took a photo directly)
[self saveImageWithPickerInfo:info image:image imageQuality:imageQuality];
Expand Down
8 changes: 8 additions & 0 deletions packages/image_picker/image_picker/lib/image_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class ImagePicker {
/// image types such as JPEG. If compression is not supported for the image that is picked,
/// an warning message will be logged.
///
/// Set `iosPhaAsset` to `false` to disable `PHAAsset` and related permissions requirements on iOS.
///
/// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
/// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
/// Defaults to [CameraDevice.rear].
Expand All @@ -57,12 +59,14 @@ class ImagePicker {
double maxWidth,
double maxHeight,
int imageQuality,
bool iosPhaAsset = true,
CameraDevice preferredCameraDevice = CameraDevice.rear}) async {
String path = await platform.pickImagePath(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: imageQuality,
iosPhaAsset: iosPhaAsset,
preferredCameraDevice: preferredCameraDevice,
);

Expand All @@ -85,6 +89,8 @@ class ImagePicker {
/// image types such as JPEG and on Android PNG and WebP, too. If compression is not supported for the image that is picked,
/// a warning message will be logged.
///
/// Set `iosPhaAsset` to `false` to disable `PHAAsset` and related permissions requirements on iOS.
///
/// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
/// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
/// Defaults to [CameraDevice.rear]. Note that Android has no documented parameter for an intent to specify if
Expand All @@ -98,13 +104,15 @@ class ImagePicker {
double maxWidth,
double maxHeight,
int imageQuality,
bool iosPhaAsset = true,
CameraDevice preferredCameraDevice = CameraDevice.rear,
}) {
return platform.pickImage(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: imageQuality,
iosPhaAsset: iosPhaAsset,
preferredCameraDevice: preferredCameraDevice,
);
}
Expand Down
29 changes: 20 additions & 9 deletions packages/image_picker/image_picker/test/image_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ void main() {
'maxWidth': null,
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 1,
'maxWidth': null,
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
],
);
Expand Down Expand Up @@ -85,49 +87,56 @@ void main() {
'maxWidth': null,
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': null,
'maxHeight': 10.0,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': 20.0,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': null,
'imageQuality': 70,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': null,
'maxHeight': 10.0,
'imageQuality': 70,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': 20.0,
'imageQuality': 70,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
],
);
Expand Down Expand Up @@ -164,6 +173,7 @@ void main() {
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0,
'iosPhaAsset': true,
}),
],
);
Expand All @@ -183,6 +193,7 @@ void main() {
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 1,
'iosPhaAsset': true,
}),
],
);
Expand Down
29 changes: 20 additions & 9 deletions packages/image_picker/image_picker/test/old_image_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ void main() {
'maxWidth': null,
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 1,
'maxWidth': null,
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
],
);
Expand Down Expand Up @@ -85,49 +87,56 @@ void main() {
'maxWidth': null,
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': null,
'maxHeight': 10.0,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': 20.0,
'imageQuality': null,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': null,
'imageQuality': 70,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': null,
'maxHeight': 10.0,
'imageQuality': 70,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': 20.0,
'imageQuality': 70,
'cameraDevice': 0
'cameraDevice': 0,
'iosPhaAsset': true,
}),
],
);
Expand Down Expand Up @@ -165,6 +174,7 @@ void main() {
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 0,
'iosPhaAsset': true,
}),
],
);
Expand All @@ -184,6 +194,7 @@ void main() {
'maxHeight': null,
'imageQuality': null,
'cameraDevice': 1,
'iosPhaAsset': true,
}),
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ImagePickerPlugin extends ImagePickerPlatform {
double maxWidth,
double maxHeight,
int imageQuality,
bool iosPhaAsset = true,
CameraDevice preferredCameraDevice = CameraDevice.rear,
}) {
String capture = computeCaptureAttribute(source, preferredCameraDevice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
double maxWidth,
double maxHeight,
int imageQuality,
bool iosPhaAsset = true,
CameraDevice preferredCameraDevice = CameraDevice.rear,
}) async {
String path = await pickImagePath(
source: source,
maxWidth: maxWidth,
maxHeight: maxHeight,
imageQuality: imageQuality,
iosPhaAsset: iosPhaAsset,
preferredCameraDevice: preferredCameraDevice,
);
return path != null ? PickedFile(path) : null;
Expand All @@ -43,6 +45,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
double maxWidth,
double maxHeight,
int imageQuality,
bool iosPhaAsset = true,
CameraDevice preferredCameraDevice = CameraDevice.rear,
}) {
assert(source != null);
Expand All @@ -66,6 +69,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
'maxWidth': maxWidth,
'maxHeight': maxHeight,
'imageQuality': imageQuality,
'iosPhaAsset': iosPhaAsset,
'cameraDevice': preferredCameraDevice.index
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ abstract class ImagePickerPlatform extends PlatformInterface {
/// image types such as JPEG and on Android PNG and WebP, too. If compression is not supported for the image that is picked,
/// a warning message will be logged.
///
/// Set `iosPhaAsset` to `false` to disable `PHAAsset` and related permissions requirements on iOS.
///
/// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
/// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
/// Defaults to [CameraDevice.rear].
Expand All @@ -66,6 +68,7 @@ abstract class ImagePickerPlatform extends PlatformInterface {
double maxWidth,
double maxHeight,
int imageQuality,
bool iosPhaAsset = true,
CameraDevice preferredCameraDevice = CameraDevice.rear,
}) {
throw UnimplementedError('legacyPickImage() has not been implemented.');
Expand Down Expand Up @@ -130,6 +133,8 @@ abstract class ImagePickerPlatform extends PlatformInterface {
/// image types such as JPEG. If compression is not supported for the image that is picked,
/// an warning message will be logged.
///
/// Set `iosPhaAsset` to `false` to disable `PHAAsset` and related permissions requirements on iOS.
///
/// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
/// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
/// Defaults to [CameraDevice.rear]. Note that Android has no documented parameter for an intent to specify if
Expand All @@ -143,6 +148,7 @@ abstract class ImagePickerPlatform extends PlatformInterface {
double maxWidth,
double maxHeight,
int imageQuality,
bool iosPhaAsset = true,
CameraDevice preferredCameraDevice = CameraDevice.rear,
}) {
throw UnimplementedError('pickImage() has not been implemented.');
Expand Down
Loading