Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -511,7 +511,10 @@ private void handleImageResult(String path, boolean shouldDeleteOriginalIfScaled
if (methodCall != null) {
Double maxWidth = methodCall.argument("maxWidth");
Double maxHeight = methodCall.argument("maxHeight");
int imageQuality = methodCall.argument("imageQuality");
int imageQuality =
methodCall.argument("imageQuality") == null
? 100
: (int) methodCall.argument("imageQuality");

String finalImagePath =
imageResizer.resizeImageIfNeeded(path, maxWidth, maxHeight, imageQuality);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -93,6 +94,11 @@ private File resizedImage(String path, Double maxWidth, Double maxHeight, int im
Bitmap scaledBmp = Bitmap.createScaledBitmap(bmp, width.intValue(), height.intValue(), false);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
boolean saveAsPNG = bmp.hasAlpha();
if (saveAsPNG) {
Log.d(
"ImageResizer",
"image_picker: compressing is not supported for type PNG. Returning the image with original quality");
}
scaledBmp.compress(
saveAsPNG ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG,
imageQuality,
Expand Down
3 changes: 1 addition & 2 deletions packages/image_picker/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class _MyHomePageState extends State<MyHomePage> {
});
} else {
try {
_imageFile =
await ImagePicker.pickImage(source: source, imageQuality: 50);
_imageFile = await ImagePicker.pickImage(source: source);
} catch (e) {
_pickImageError = e;
}
Expand Down
12 changes: 3 additions & 9 deletions packages/image_picker/ios/Classes/FLTImagePickerMetaDataUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,9 @@ + (NSData *)convertImage:(UIImage *)image
usingType:(FLTImagePickerMIMEType)type
quality:(nullable NSNumber *)quality {

// Commenting as of now as JPEG conversion is done by default
// if (quality && type != FLTImagePickerMIMETypeJPEG) {
// @throw [NSException
// exceptionWithName:@"flutter_image_picker_convert_image_exception"
// reason:[NSString stringWithFormat:@"quality is not available for type %@",
// [FLTImagePickerMetaDataUtil
// imageTypeSuffixFromType:type]]
// userInfo:nil];
// }
if (quality && type != FLTImagePickerMIMETypeJPEG) {
NSLog(@"image_picker: compressing is not supported for type %@. Returning the image with original quality");
Copy link
Contributor

Choose a reason for hiding this comment

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

add argument for type.

NSLog(@"image_picker: compressing is not supported for type %@. Returning the image with original quality", [FLTImagePickerMetaDataUtil imageTypeSuffixFromType:type]});

I typed this code on Github site so it might fail compilation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

switch (type) {
case FLTImagePickerMIMETypeJPEG: {
Expand Down
4 changes: 4 additions & 0 deletions packages/image_picker/ios/Classes/ImagePickerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ - (void)imagePickerController:(UIImagePickerController *)picker
NSNumber *maxHeight = [_arguments objectForKey:@"maxHeight"];
NSNumber *imageQuality = [_arguments objectForKey:@"imageQuality"];

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add

if (![ImageQuality isKindOfClass:[NSNumber class]]) {
   imageQuality = @1;
} 

This made the imageQuality an optional parameter and returns 1 to be default. Same on Android

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if (![imageQuality isKindOfClass:[NSNumber class]]) {
imageQuality = @1;
}

if (imageQuality.intValue < 0 || imageQuality.intValue > 100) {
Copy link
Contributor

Choose a reason for hiding this comment

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

make it else if. in iOS nil.intValue will return 0 which is not what we wanted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

imageQuality = [NSNumber numberWithInt:1];
} else {
Expand Down
10 changes: 1 addition & 9 deletions packages/image_picker/lib/image_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ImagePicker {
double maxHeight,
int imageQuality}) async {
assert(source != null);
assert(imageQuality == null || (imageQuality >= 0 && imageQuality <= 100));

if (maxWidth != null && maxWidth < 0) {
throw ArgumentError.value(maxWidth, 'maxWidth cannot be negative');
Expand All @@ -53,15 +54,6 @@ class ImagePicker {
throw ArgumentError.value(maxHeight, 'maxHeight cannot be negative');
}

if (null == imageQuality) {
imageQuality = 100;
}

if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
throw ArgumentError.value(
imageQuality, 'invalid imageQuality, must be between 0 and 100');
}

final String path = await _channel.invokeMethod<String>(
'pickImage',
<String, dynamic>{
Expand Down
12 changes: 6 additions & 6 deletions packages/image_picker/test/image_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ void main() {
'source': 0,
'maxWidth': null,
'maxHeight': null,
'imageQuality': 100
'imageQuality': null
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 1,
'maxWidth': null,
'maxHeight': null,
'imageQuality': 100
'imageQuality': null
}),
],
);
Expand Down Expand Up @@ -78,25 +78,25 @@ void main() {
'source': 0,
'maxWidth': null,
'maxHeight': null,
'imageQuality': 100
'imageQuality': null
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': null,
'imageQuality': 100
'imageQuality': null
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': null,
'maxHeight': 10.0,
'imageQuality': 100
'imageQuality': null
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': 20.0,
'imageQuality': 100
'imageQuality': null
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
Expand Down