This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[image_picker] iOS: save image to the correct type based on its original type and copy over exif data from the original image #1586
Merged
cyanglaz
merged 2 commits into
flutter:master
from
cyanglaz:image_picker_ios_file_type_refactor
May 20, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 187 additions & 26 deletions
213
packages/image_picker/example/ios/Runner.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/image_picker/example/ios/Runner/Assets.xcassets/Contents.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "info" : { | ||
| "version" : 1, | ||
| "author" : "xcode" | ||
| } | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions
22
packages/image_picker/example/ios/image_picker_exampleTests/Info.plist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>CFBundleDevelopmentRegion</key> | ||
| <string>$(DEVELOPMENT_LANGUAGE)</string> | ||
| <key>CFBundleExecutable</key> | ||
| <string>$(EXECUTABLE_NAME)</string> | ||
| <key>CFBundleIdentifier</key> | ||
| <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
| <key>CFBundleInfoDictionaryVersion</key> | ||
| <string>6.0</string> | ||
| <key>CFBundleName</key> | ||
| <string>$(PRODUCT_NAME)</string> | ||
| <key>CFBundlePackageType</key> | ||
| <string>BNDL</string> | ||
| <key>CFBundleShortVersionString</key> | ||
| <string>1.0</string> | ||
| <key>CFBundleVersion</key> | ||
| <string>1</string> | ||
| </dict> | ||
| </plist> |
107 changes: 107 additions & 0 deletions
107
packages/image_picker/example/ios/image_picker_exampleTests/MetaDataUtilTests.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #import <XCTest/XCTest.h> | ||
| #import "ImagePickerMetaDataUtil.h" | ||
|
|
||
| @interface MetaDataUtilTests : XCTestCase | ||
|
|
||
| @property(strong, nonatomic) NSBundle *testBundle; | ||
|
|
||
| @end | ||
|
|
||
| @implementation MetaDataUtilTests | ||
|
|
||
| - (void)setUp { | ||
| self.testBundle = [NSBundle bundleForClass:self.class]; | ||
| } | ||
|
|
||
| - (void)testGetImageMIMETypeFromImageData { | ||
| // test jpeg | ||
| NSData *dataJPG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"jpgImage" | ||
| ofType:@"jpg"]]; | ||
| XCTAssertEqual([ImagePickerMetaDataUtil getImageMIMETypeFromImageData:dataJPG], | ||
| FlutterImagePickerMIMETypeJPEG); | ||
|
|
||
| // test png | ||
| NSData *dataPNG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"pngImage" | ||
| ofType:@"png"]]; | ||
| XCTAssertEqual([ImagePickerMetaDataUtil getImageMIMETypeFromImageData:dataPNG], | ||
| FlutterImagePickerMIMETypePNG); | ||
| } | ||
|
|
||
| - (void)testSuffixFromType { | ||
| // test jpeg | ||
| XCTAssertEqualObjects( | ||
| [ImagePickerMetaDataUtil imageTypeSuffixFromType:FlutterImagePickerMIMETypeJPEG], @".jpg"); | ||
|
|
||
| // test png | ||
| XCTAssertEqualObjects( | ||
| [ImagePickerMetaDataUtil imageTypeSuffixFromType:FlutterImagePickerMIMETypePNG], @".png"); | ||
|
|
||
| // test other | ||
| XCTAssertNil([ImagePickerMetaDataUtil imageTypeSuffixFromType:FlutterImagePickerMIMETypeOther]); | ||
| } | ||
|
|
||
| - (void)testGetMetaData { | ||
| NSData *dataJPG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"jpgImage" | ||
| ofType:@"jpg"]]; | ||
| NSDictionary *metaData = [ImagePickerMetaDataUtil getMetaDataFromImageData:dataJPG]; | ||
| NSDictionary *exif = [metaData objectForKey:(NSString *)kCGImagePropertyExifDictionary]; | ||
| XCTAssertEqual([exif[(NSString *)kCGImagePropertyExifPixelXDimension] integerValue], 12); | ||
| } | ||
|
|
||
| - (void)testGetEXIFData { | ||
| NSData *dataJPG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"jpgImage" | ||
| ofType:@"jpg"]]; | ||
| NSDictionary *exif = [ImagePickerMetaDataUtil getEXIFFromImageData:dataJPG]; | ||
| XCTAssertNotNil(exif); | ||
| // hard coded test case based on the test image file saved in directory. | ||
| XCTAssertEqualObjects(exif[@"PixelXDimension"], @(12)); | ||
| XCTAssertEqualObjects(exif[@"PixelYDimension"], @(7)); | ||
| } | ||
|
|
||
| - (void)testWriteEXIFData { | ||
| NSData *dataJPG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"jpgImage" | ||
| ofType:@"jpg"]]; | ||
| NSDictionary *metaData = [ImagePickerMetaDataUtil getMetaDataFromImageData:dataJPG]; | ||
| NSDictionary *exif = [metaData objectForKey:(NSString *)kCGImagePropertyExifDictionary]; | ||
| NSString *tmpFile = [NSString stringWithFormat:@"image_picker_test.jpg"]; | ||
| NSString *tmpDirectory = NSTemporaryDirectory(); | ||
| NSString *tmpPath = [tmpDirectory stringByAppendingPathComponent:tmpFile]; | ||
| NSData *newData = [ImagePickerMetaDataUtil updateEXIFData:exif toImage:dataJPG]; | ||
| if ([[NSFileManager defaultManager] createFileAtPath:tmpPath contents:newData attributes:nil]) { | ||
| NSData *savedTmpImageData = [NSData dataWithContentsOfFile:tmpPath]; | ||
| NSDictionary *tmpMetaData = | ||
| [ImagePickerMetaDataUtil getMetaDataFromImageData:savedTmpImageData]; | ||
| NSDictionary *tmpExif = [tmpMetaData objectForKey:(NSString *)kCGImagePropertyExifDictionary]; | ||
| XCTAssert([tmpExif isEqualToDictionary:exif]); | ||
| } else { | ||
| XCTAssert(NO); | ||
| } | ||
| } | ||
|
|
||
| - (void)testConvertImageToData { | ||
| NSData *dataJPG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"jpgImage" | ||
| ofType:@"jpg"]]; | ||
| UIImage *imageJPG = [UIImage imageWithData:dataJPG]; | ||
| NSData *convertedDataJPG = [ImagePickerMetaDataUtil convertImage:imageJPG | ||
| usingType:FlutterImagePickerMIMETypeJPEG | ||
| quality:@(0.5)]; | ||
| XCTAssertEqual([ImagePickerMetaDataUtil getImageMIMETypeFromImageData:convertedDataJPG], | ||
| FlutterImagePickerMIMETypeJPEG); | ||
|
|
||
| NSData *convertedDataPNG = [ImagePickerMetaDataUtil convertImage:imageJPG | ||
| usingType:FlutterImagePickerMIMETypePNG | ||
| quality:nil]; | ||
| XCTAssertEqual([ImagePickerMetaDataUtil getImageMIMETypeFromImageData:convertedDataPNG], | ||
| FlutterImagePickerMIMETypePNG); | ||
|
|
||
| // test throws exceptions | ||
| XCTAssertThrows([ImagePickerMetaDataUtil convertImage:imageJPG | ||
| usingType:FlutterImagePickerMIMETypePNG | ||
| quality:@(0.5)], | ||
| @"setting quality when converting to PNG throws exception"); | ||
| } | ||
| @end |
70 changes: 70 additions & 0 deletions
70
packages/image_picker/example/ios/image_picker_exampleTests/PhotoAssetUtilTests.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #import <XCTest/XCTest.h> | ||
| #import "ImagePickerMetaDataUtil.h" | ||
| #import "ImagePickerPhotoAssetUtil.h" | ||
|
|
||
| @interface PhotoAssetUtilTests : XCTestCase | ||
|
|
||
| @property(strong, nonatomic) NSBundle *testBundle; | ||
|
|
||
| @end | ||
|
|
||
| @implementation PhotoAssetUtilTests | ||
|
|
||
| - (void)setUp { | ||
| self.testBundle = [NSBundle bundleForClass:self.class]; | ||
| } | ||
|
|
||
| - (void)testSaveImageWithOriginalImageData_ShouldSaveWithTheCorrectExtentionAndMetaData { | ||
| // test jpg | ||
| NSData *dataJPG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"jpgImage" | ||
| ofType:@"jpg"]]; | ||
| UIImage *imageJPG = [UIImage imageWithData:dataJPG]; | ||
| NSString *savedPathJPG = [ImagePickerPhotoAssetUtil saveImageWithOriginalImageData:dataJPG | ||
| image:imageJPG]; | ||
| XCTAssertNotNil(savedPathJPG); | ||
| XCTAssertEqualObjects([savedPathJPG substringFromIndex:savedPathJPG.length - 4], @".jpg"); | ||
|
|
||
| // test png | ||
| NSData *dataPNG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"pngImage" | ||
| ofType:@"png"]]; | ||
| UIImage *imagePNG = [UIImage imageWithData:dataPNG]; | ||
| NSString *savedPathPNG = [ImagePickerPhotoAssetUtil saveImageWithOriginalImageData:dataPNG | ||
| image:imagePNG]; | ||
| XCTAssertNotNil(savedPathPNG); | ||
| XCTAssertEqualObjects([savedPathPNG substringFromIndex:savedPathPNG.length - 4], @".png"); | ||
| } | ||
|
|
||
| - (void)testSaveImageWithPickerInfo_ShouldSaveWithDefaultExtention { | ||
| NSData *dataJPG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"jpgImage" | ||
| ofType:@"jpg"]]; | ||
| UIImage *imageJPG = [UIImage imageWithData:dataJPG]; | ||
| NSString *savedPathJPG = [ImagePickerPhotoAssetUtil saveImageWithPickerInfo:nil image:imageJPG]; | ||
|
|
||
| XCTAssertNotNil(savedPathJPG); | ||
| // should be saved as | ||
| XCTAssertEqualObjects([savedPathJPG substringFromIndex:savedPathJPG.length - 4], | ||
| kFlutterImagePickerDefaultSuffix); | ||
| } | ||
|
|
||
| - (void)testSaveImageWithPickerInfo_ShouldSaveWithTheCorrectExtentionAndMetaData { | ||
| NSDictionary *dummyInfo = @{ | ||
| UIImagePickerControllerMediaMetadata : @{ | ||
| (__bridge NSString *)kCGImagePropertyExifDictionary : | ||
| @{(__bridge NSString *)kCGImagePropertyExifMakerNote : @"aNote"} | ||
| } | ||
| }; | ||
| NSData *dataJPG = [NSData dataWithContentsOfFile:[self.testBundle pathForResource:@"jpgImage" | ||
| ofType:@"jpg"]]; | ||
| UIImage *imageJPG = [UIImage imageWithData:dataJPG]; | ||
| NSString *savedPathJPG = [ImagePickerPhotoAssetUtil saveImageWithPickerInfo:dummyInfo | ||
| image:imageJPG]; | ||
| NSData *data = [NSData dataWithContentsOfFile:savedPathJPG]; | ||
| NSDictionary *exif = [ImagePickerMetaDataUtil getEXIFFromImageData:data]; | ||
| XCTAssertEqualObjects(exif[(__bridge NSString *)kCGImagePropertyExifMakerNote], @"aNote"); | ||
| } | ||
|
|
||
| @end |
44 changes: 44 additions & 0 deletions
44
packages/image_picker/ios/Classes/ImagePickerMetaDataUtil.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #import <Foundation/Foundation.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| typedef enum : NSUInteger { | ||
| FlutterImagePickerMIMETypePNG, | ||
| FlutterImagePickerMIMETypeJPEG, | ||
| FlutterImagePickerMIMETypeOther, | ||
| } FlutterImagePickerMIMEType; | ||
|
|
||
| extern NSString *const kFlutterImagePickerDefaultSuffix; | ||
| extern const FlutterImagePickerMIMEType kFlutterImagePickerMIMETypeDefault; | ||
|
|
||
| @interface ImagePickerMetaDataUtil : NSObject | ||
|
|
||
| // Retrieve MIME type by reading the image data. We currently only support some popular types. | ||
| + (FlutterImagePickerMIMEType)getImageMIMETypeFromImageData:(NSData *)imageData; | ||
|
|
||
| // Get corresponding surfix from type. | ||
| + (NSString *)imageTypeSuffixFromType:(FlutterImagePickerMIMEType)type; | ||
|
|
||
| + (NSDictionary *)getMetaDataFromImageData:(NSData *)imageData; | ||
|
|
||
| + (NSDictionary *)getEXIFFromImageData:(NSData *)imageData; | ||
|
|
||
| + (NSData *)updateEXIFData:(NSDictionary *)exifData toImage:(NSData *)imageData; | ||
|
|
||
| // Converting UIImage to a NSData with the type proveide. | ||
| // | ||
| // The quality is for JPEG type only, it defaults to 1. It throws exception if setting a non-nil | ||
| // quality with type other than FlutterImagePickerMIMETypeJPEG. Converting UIImage to | ||
| // FlutterImagePickerMIMETypeGIF or FlutterImagePickerMIMETypeTIFF is not supported in iOS. This | ||
| // method throws exception if trying to do so. | ||
| + (nonnull NSData *)convertImage:(nonnull UIImage *)image | ||
| usingType:(FlutterImagePickerMIMEType)type | ||
| quality:(nullable NSNumber *)quality; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the argument type here is not
UIImagebutNSData, it can handle gif images.Please refer to the following.
https://github.com/flutter/plugins/pull/866/files#diff-ce2e9c98db20f69ecf981788585d7a87R209
This will be able to also handle animated gif.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can add the gif handling in a separate PR. I also want to take a deeper dive into the implementation to see if there is a way to support scaling gif. If not, then we need to decide how to handle when people passed the scaling factors into the API.