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
Next Next commit
[image_picker] Check for failure in iOS metadata updates
Reworks the logic in updateMetaData:toImage: to correctly handle the
possibility of null return values from intermediate steps. Also renames
it to have a more idiomatic ObjC name.

Speculative fix for flutter/flutter#80429
Since the case this would likely fix is bad image data, this may simply
move problems to another part of the plugin, but this is definitely a
correctness improvement here, and if it does move the issue elsewhere we
can tackle that once we have more information.
  • Loading branch information
stuartmorgan-g committed Aug 3, 2021
commit d1917b1f28d827d4562ee3d71820782cbff0fe67
7 changes: 4 additions & 3 deletions packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 0.8.3

* Move `ImagePickerFromLimitedGalleryUITests` to `RunnerUITests` target.
* Improved handling of bad image data when applying metadata changes on iOS.

## 0.8.2

Expand Down Expand Up @@ -53,8 +54,8 @@ see: [#84634](https://github.com/flutter/flutter/issues/84634).
## 0.8.0

* BREAKING CHANGE: Changed storage location for captured images and videos to internal cache on Android,
to comply with new Google Play storage requirements. This means developers are responsible for moving
the image or video to a different location in case more permanent storage is required. Other applications
to comply with new Google Play storage requirements. This means developers are responsible for moving
the image or video to a different location in case more permanent storage is required. Other applications
will no longer be able to access images or videos captured unless they are moved to a publicly accessible location.
* Updated Mockito to fix Android tests.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ - (void)testWriteMetaData {
NSString *tmpFile = [NSString stringWithFormat:@"image_picker_test.jpg"];
NSString *tmpDirectory = NSTemporaryDirectory();
NSString *tmpPath = [tmpDirectory stringByAppendingPathComponent:tmpFile];
NSData *newData = [FLTImagePickerMetaDataUtil updateMetaData:metaData toImage:dataJPG];
NSData *newData = [FLTImagePickerMetaDataUtil imageFromImage:dataJPG withMetaData:metaData];
if ([[NSFileManager defaultManager] createFileAtPath:tmpPath contents:newData attributes:nil]) {
NSData *savedTmpImageData = [NSData dataWithContentsOfFile:tmpPath];
NSDictionary *tmpMetaData =
Expand All @@ -71,6 +71,14 @@ - (void)testWriteMetaData {
}
}

- (void)testUpdateMetaDataBadData {
NSData *imageData = [NSData data];

NSDictionary *metaData = [FLTImagePickerMetaDataUtil getMetaDataFromImageData:imageData];
NSData *newData = [FLTImagePickerMetaDataUtil imageFromImage:imageData withMetaData:metaData];
XCTAssertNil(newData);
}

- (void)testConvertImageToData {
UIImage *imageJPG = [UIImage imageWithData:ImagePickerTestImages.JPGTestData];
NSData *convertedDataJPG = [FLTImagePickerMetaDataUtil convertImage:imageJPG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ extern const FLTImagePickerMIMEType kFLTImagePickerMIMETypeDefault;

+ (NSDictionary *)getMetaDataFromImageData:(NSData *)imageData;

+ (NSData *)updateMetaData:(NSDictionary *)metaData toImage:(NSData *)imageData;
// Creates and returns data for a new image based on imageData, but with the
// given metadata.
//
// If creating a new image fails, returns nil.
+ (nullable NSData *)imageFromImage:(NSData *)imageData withMetaData:(NSDictionary *)metadata;

// Converting UIImage to a NSData with the type proveide.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,27 @@ + (NSDictionary *)getMetaDataFromImageData:(NSData *)imageData {
return metadata;
}

+ (NSData *)updateMetaData:(NSDictionary *)metaData toImage:(NSData *)imageData {
NSMutableData *mutableData = [NSMutableData data];
CGImageSourceRef cgImage = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CGImageDestinationRef destination = CGImageDestinationCreateWithData(
(__bridge CFMutableDataRef)mutableData, CGImageSourceGetType(cgImage), 1, nil);
CGImageDestinationAddImageFromSource(destination, cgImage, 0, (__bridge CFDictionaryRef)metaData);
+ (NSData *)imageFromImage:(NSData *)imageData withMetaData:(NSDictionary *)metadata {
NSMutableData *targetData = [NSMutableData data];
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
if (source == NULL) {
return nil;
}
CGImageDestinationRef destination = NULL;
CFStringRef sourceType = CGImageSourceGetType(source);
if (sourceType != NULL) {
destination =
CGImageDestinationCreateWithData((__bridge CFMutableDataRef)targetData, sourceType, 1, nil);
}
if (destination == NULL) {
CFRelease(source);
return nil;
}
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metadata);
CGImageDestinationFinalize(destination);
CFRelease(cgImage);
CFRelease(source);
CFRelease(destination);
return mutableData;
return targetData;
}

+ (NSData *)convertImage:(UIImage *)image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ + (NSString *)saveImageWithMetaData:(NSDictionary *)metaData
usingType:type
quality:imageQuality];
if (metaData) {
data = [FLTImagePickerMetaDataUtil updateMetaData:metaData toImage:data];
NSData *updatedData = [FLTImagePickerMetaDataUtil imageFromImage:data withMetaData:metaData];
// If updating the metadata fails, just save the original.
if (updatedData) {
data = updatedData;
}
}

return [self createFile:data suffix:suffix];
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/master/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.2
version: 0.8.3

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