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

[image_picker] Image picker fix metadata #3873

Merged
merged 9 commits into from
May 13, 2021
Merged
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
5 changes: 5 additions & 0 deletions packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.7.5+1

* Fixes a rotation problem where Select Photos limited access is chosen but the image that is picked
is not included selected photos and image is scaled.

## 0.7.5

* Fixes an issue where image rotation is wrong when Select Photos chose and image is scaled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ NS_ASSUME_NONNULL_BEGIN

+ (UIImage *)scaledImage:(UIImage *)image
maxWidth:(NSNumber *)maxWidth
maxHeight:(NSNumber *)maxHeight;
maxHeight:(NSNumber *)maxHeight
isMetadataAvailable:(BOOL)isMetadataAvailable;

// Resize all gif animation frames.
+ (GIFInfo *)scaledGIFImage:(NSData *)data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ @implementation FLTImagePickerImageUtil : NSObject

+ (UIImage *)scaledImage:(UIImage *)image
maxWidth:(NSNumber *)maxWidth
maxHeight:(NSNumber *)maxHeight {
maxHeight:(NSNumber *)maxHeight
isMetadataAvailable:(BOOL)isMetadataAvailable {
double originalWidth = image.size.width;
double originalHeight = image.size.height;

Expand Down Expand Up @@ -69,6 +70,19 @@ + (UIImage *)scaledImage:(UIImage *)image
}
}

if (!isMetadataAvailable) {
UIImage *imageToScale = [UIImage imageWithCGImage:image.CGImage
scale:1
orientation:image.imageOrientation];

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

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}

// 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.
Expand Down Expand Up @@ -130,7 +144,7 @@ + (GIFInfo *)scaledGIFImage:(NSData *)data
}

UIImage *image = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];
image = [self scaledImage:image maxWidth:maxWidth maxHeight:maxHeight];
image = [self scaledImage:image maxWidth:maxWidth maxHeight:maxHeight isMetadataAvailable:YES];

[images addObject:image];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,16 @@ - (void)picker:(PHPickerViewController *)picker
if (image != nil) {
__block UIImage *localImage = image;
dispatch_async(dispatch_get_main_queue(), ^{
PHAsset *originalAsset =
[FLTImagePickerPhotoAssetUtil getAssetFromPHPickerResult:result];

if (maxWidth != (id)[NSNull null] || maxHeight != (id)[NSNull null]) {
localImage = [FLTImagePickerImageUtil scaledImage:localImage
maxWidth:maxWidth
maxHeight:maxHeight];
maxHeight:maxHeight
isMetadataAvailable:originalAsset != nil];
}

PHAsset *originalAsset =
[FLTImagePickerPhotoAssetUtil getAssetFromPHPickerResult:result];

if (!originalAsset) {
// Image picked without an original asset (e.g. User took a photo directly)
[self saveImageWithPickerInfo:nil
Expand Down Expand Up @@ -449,11 +450,15 @@ - (void)imagePickerController:(UIImagePickerController *)picker
NSNumber *imageQuality = [_arguments objectForKey:@"imageQuality"];
NSNumber *desiredImageQuality = [self getDesiredImageQuality:imageQuality];

PHAsset *originalAsset = [FLTImagePickerPhotoAssetUtil getAssetFromImagePickerInfo:info];

if (maxWidth != (id)[NSNull null] || maxHeight != (id)[NSNull null]) {
image = [FLTImagePickerImageUtil scaledImage:image maxWidth:maxWidth maxHeight:maxHeight];
image = [FLTImagePickerImageUtil scaledImage:image
maxWidth:maxWidth
maxHeight:maxHeight
isMetadataAvailable:originalAsset != nil];
}

PHAsset *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:desiredImageQuality];
Expand Down
16 changes: 15 additions & 1 deletion packages/image_picker/image_picker/ios/Tests/ImageUtilTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,21 @@ @implementation ImageUtilTests

- (void)testScaledImage_ShouldBeScaled {
UIImage *image = [UIImage imageWithData:ImagePickerTestImages.JPGTestData];
UIImage *newImage = [FLTImagePickerImageUtil scaledImage:image maxWidth:@3 maxHeight:@2];
UIImage *newImage = [FLTImagePickerImageUtil scaledImage:image
maxWidth:@3
maxHeight:@2
isMetadataAvailable:YES];

XCTAssertEqual(newImage.size.width, 3);
XCTAssertEqual(newImage.size.height, 2);
}

- (void)testScaledImage_ShouldBeScaledWithNoMetadata {
UIImage *image = [UIImage imageWithData:ImagePickerTestImages.JPGTestData];
UIImage *newImage = [FLTImagePickerImageUtil scaledImage:image
maxWidth:@3
maxHeight:@2
isMetadataAvailable:NO];

XCTAssertEqual(newImage.size.width, 3);
XCTAssertEqual(newImage.size.height, 2);
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 @@ -2,7 +2,7 @@ name: image_picker
description: Flutter plugin for selecting images from the Android and iOS image
library, and taking new pictures with the camera.
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker
version: 0.7.5
version: 0.7.5+1

flutter:
plugin:
Expand Down