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

Commit

Permalink
[image_picker] Fix NSNull handling on iOS (#4728)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmorgan authored Feb 4, 2022
1 parent 37b592c commit 9b2d46a
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 17 deletions.
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.4+6

* Fixes minor type issues in iOS implementation.

## 0.8.4+5

* Improves the documentation on handling MainActivity being killed by the Android OS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 50;
objects = {

/* Begin PBXBuildFile section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ - (void)testSaveImageWithOriginalImageData_ShouldSaveAsGifAnimation {

size_t numberOfFrames = CGImageSourceGetCount(imageSource);

NSNumber *nilSize = (NSNumber *)[NSNull null];
NSString *savedPathGIF = [FLTImagePickerPhotoAssetUtil saveImageWithOriginalImageData:dataGIF
image:imageGIF
maxWidth:nilSize
maxHeight:nilSize
maxWidth:nil
maxHeight:nil
imageQuality:nil];
XCTAssertNotNil(savedPathGIF);
XCTAssertEqualObjects([savedPathGIF substringFromIndex:savedPathGIF.length - 4], @".gif");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ NS_ASSUME_NONNULL_BEGIN

@interface FLTImagePickerImageUtil : NSObject

// Resizes the given image to fit within maxWidth (if non-nil) and maxHeight (if non-nil)
+ (UIImage *)scaledImage:(UIImage *)image
maxWidth:(NSNumber *)maxWidth
maxHeight:(NSNumber *)maxHeight
maxWidth:(nullable NSNumber *)maxWidth
maxHeight:(nullable NSNumber *)maxHeight
isMetadataAvailable:(BOOL)isMetadataAvailable;

// Resize all gif animation frames.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ + (UIImage *)scaledImage:(UIImage *)image
double originalWidth = image.size.width;
double originalHeight = image.size.height;

bool hasMaxWidth = maxWidth != (id)[NSNull null];
bool hasMaxHeight = maxHeight != (id)[NSNull null];
bool hasMaxWidth = maxWidth != nil;
bool hasMaxHeight = maxHeight != nil;

double width = hasMaxWidth ? MIN([maxWidth doubleValue], originalWidth) : originalWidth;
double height = hasMaxHeight ? MIN([maxHeight doubleValue], originalHeight) : originalHeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
#import "FLTImagePickerPhotoAssetUtil.h"
#import "FLTPHPickerSaveImageToPathOperation.h"

/**
* Returns the value for the given key in 'dict', or nil if the value is
* NSNull.
*/
id GetNullableValueForKey(NSDictionary *dict, NSString *key) {
id value = dict[key];
return value == [NSNull null] ? nil : value;
}

@interface FLTImagePickerPlugin () <UINavigationControllerDelegate,
UIImagePickerControllerDelegate,
PHPickerViewControllerDelegate,
Expand Down Expand Up @@ -397,9 +406,9 @@ - (void)picker:(PHPickerViewController *)picker
dispatch_queue_t backgroundQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(backgroundQueue, ^{
NSNumber *maxWidth = [self->_arguments objectForKey:@"maxWidth"];
NSNumber *maxHeight = [self->_arguments objectForKey:@"maxHeight"];
NSNumber *imageQuality = [self->_arguments objectForKey:@"imageQuality"];
NSNumber *maxWidth = GetNullableValueForKey(self->_arguments, @"maxWidth");
NSNumber *maxHeight = GetNullableValueForKey(self->_arguments, @"maxHeight");
NSNumber *imageQuality = GetNullableValueForKey(self->_arguments, @"imageQuality");
NSNumber *desiredImageQuality = [self getDesiredImageQuality:imageQuality];
NSOperationQueue *operationQueue = [NSOperationQueue new];
NSMutableArray *pathList = [self createNSMutableArrayWithSize:results.count];
Expand Down Expand Up @@ -480,14 +489,14 @@ - (void)imagePickerController:(UIImagePickerController *)picker
if (image == nil) {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
NSNumber *maxWidth = [_arguments objectForKey:@"maxWidth"];
NSNumber *maxHeight = [_arguments objectForKey:@"maxHeight"];
NSNumber *imageQuality = [_arguments objectForKey:@"imageQuality"];
NSNumber *maxWidth = GetNullableValueForKey(_arguments, @"maxWidth");
NSNumber *maxHeight = GetNullableValueForKey(_arguments, @"maxHeight");
NSNumber *imageQuality = GetNullableValueForKey(_arguments, @"imageQuality");
NSNumber *desiredImageQuality = [self getDesiredImageQuality:imageQuality];

PHAsset *originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];

if (maxWidth != (id)[NSNull null] || maxHeight != (id)[NSNull null]) {
if (maxWidth != nil || maxHeight != nil) {
image = [FLTImagePickerImageUtil scaledImage:image
maxWidth:maxWidth
maxHeight:maxHeight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ - (void)start {
PHAsset *originalAsset =
[FLTImagePickerPhotoAssetUtil getAssetFromPHPickerResult:self.result];

if (self.maxWidth != (id)[NSNull null] || self.maxHeight != (id)[NSNull null]) {
if (self.maxWidth != nil || self.maxHeight != nil) {
localImage = [FLTImagePickerImageUtil scaledImage:localImage
maxWidth:self.maxWidth
maxHeight:self.maxHeight
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for selecting images from the Android and iOS image
library, and taking new pictures with the camera.
repository: https://github.com/flutter/plugins/tree/main/packages/image_picker/image_picker
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
version: 0.8.4+5
version: 0.8.4+6

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down

0 comments on commit 9b2d46a

Please sign in to comment.