Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
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
Next Next commit
Fix image_picker scaling of images which were taken in portrait mode.
  • Loading branch information
Xavjer committed Sep 13, 2019
commit a9eddf19de6935670c602ef6760e342edd73fdd2
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ Audrius Karosevicius <[email protected]>
Lukasz Piliszczuk <[email protected]>
SoundReply Solutions GmbH <[email protected]>
Rafal Wachol <[email protected]>
Pau Picas <[email protected]>
Pau Picas <[email protected]>
Cédric Wyss <[email protected]>
4 changes: 4 additions & 0 deletions packages/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.1+5

* iOS: Fix image_picker scaling of images which were taken in portrait mode.

## 0.6.1+4

* Android: Fix a regression where the `retrieveLostImage` does not work anymore.
Expand Down
63 changes: 12 additions & 51 deletions packages/image_picker/ios/Classes/FLTImagePickerImageUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,18 @@ @implementation FLTImagePickerImageUtil : NSObject
+ (UIImage *)scaledImage:(UIImage *)image
maxWidth:(NSNumber *)maxWidth
maxHeight:(NSNumber *)maxHeight {
double originalWidth = image.size.width;
double originalHeight = image.size.height;

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

double width = hasMaxWidth ? MIN([maxWidth doubleValue], originalWidth) : originalWidth;
double height = hasMaxHeight ? MIN([maxHeight doubleValue], originalHeight) : originalHeight;

bool shouldDownscaleWidth = hasMaxWidth && [maxWidth doubleValue] < originalWidth;
bool shouldDownscaleHeight = hasMaxHeight && [maxHeight doubleValue] < originalHeight;
bool shouldDownscale = shouldDownscaleWidth || shouldDownscaleHeight;

if (shouldDownscale) {
double downscaledWidth = floor((height / originalHeight) * originalWidth);
double downscaledHeight = floor((width / originalWidth) * originalHeight);

if (width < height) {
if (!hasMaxWidth) {
width = downscaledWidth;
} else {
height = downscaledHeight;
}
} else if (height < width) {
if (!hasMaxHeight) {
height = downscaledHeight;
} else {
width = downscaledWidth;
}
} else {
if (originalWidth < originalHeight) {
width = downscaledWidth;
} else if (originalHeight < originalWidth) {
height = downscaledHeight;
}
}
}

// Scaling the image always rotate itself based on the current imageOrientation of the original
// Image. Set to orientationUp for the orignal image before scaling, so the scaled image doesn't
// mess up with the pixels.
UIImage *imageToScale = [UIImage imageWithCGImage:image.CGImage
scale:1
orientation:UIImageOrientationUp];

UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 1.0);
[imageToScale drawInRect:CGRectMake(0, 0, width, height)];

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : MAX(maxWidth, maxHeight)
};

CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
UIImage *scaled = [UIImage imageWithCGImage:scaledImageRef];
CGImageRelease(scaledImageRef);
return scaled;
}

+ (GIFInfo *)scaledGIFImage:(NSData *)data
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors:
- Flutter Team <[email protected]>
- Rhodes Davis Jr. <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker
version: 0.6.1+4
version: 0.6.1+5

flutter:
plugin:
Expand Down