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

Commit 643c928

Browse files
[image_picker] Check for failure in iOS metadata updates (#4215)
1 parent 1fc3d92 commit 643c928

File tree

6 files changed

+43
-15
lines changed

6 files changed

+43
-15
lines changed

packages/image_picker/image_picker/CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## NEXT
1+
## 0.8.3
22

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

56
## 0.8.2
67

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

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

packages/image_picker/image_picker/example/ios/RunnerTests/MetaDataUtilTests.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ - (void)testWriteMetaData {
6060
NSString *tmpFile = [NSString stringWithFormat:@"image_picker_test.jpg"];
6161
NSString *tmpDirectory = NSTemporaryDirectory();
6262
NSString *tmpPath = [tmpDirectory stringByAppendingPathComponent:tmpFile];
63-
NSData *newData = [FLTImagePickerMetaDataUtil updateMetaData:metaData toImage:dataJPG];
63+
NSData *newData = [FLTImagePickerMetaDataUtil imageFromImage:dataJPG withMetaData:metaData];
6464
if ([[NSFileManager defaultManager] createFileAtPath:tmpPath contents:newData attributes:nil]) {
6565
NSData *savedTmpImageData = [NSData dataWithContentsOfFile:tmpPath];
6666
NSDictionary *tmpMetaData =
@@ -71,6 +71,14 @@ - (void)testWriteMetaData {
7171
}
7272
}
7373

74+
- (void)testUpdateMetaDataBadData {
75+
NSData *imageData = [NSData data];
76+
77+
NSDictionary *metaData = [FLTImagePickerMetaDataUtil getMetaDataFromImageData:imageData];
78+
NSData *newData = [FLTImagePickerMetaDataUtil imageFromImage:imageData withMetaData:metaData];
79+
XCTAssertNil(newData);
80+
}
81+
7482
- (void)testConvertImageToData {
7583
UIImage *imageJPG = [UIImage imageWithData:ImagePickerTestImages.JPGTestData];
7684
NSData *convertedDataJPG = [FLTImagePickerMetaDataUtil convertImage:imageJPG

packages/image_picker/image_picker/ios/Classes/FLTImagePickerMetaDataUtil.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ extern const FLTImagePickerMIMEType kFLTImagePickerMIMETypeDefault;
2727

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

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

3236
// Converting UIImage to a NSData with the type proveide.
3337
//

packages/image_picker/image_picker/ios/Classes/FLTImagePickerMetaDataUtil.m

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,27 @@ + (NSDictionary *)getMetaDataFromImageData:(NSData *)imageData {
4949
return metadata;
5050
}
5151

52-
+ (NSData *)updateMetaData:(NSDictionary *)metaData toImage:(NSData *)imageData {
53-
NSMutableData *mutableData = [NSMutableData data];
54-
CGImageSourceRef cgImage = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
55-
CGImageDestinationRef destination = CGImageDestinationCreateWithData(
56-
(__bridge CFMutableDataRef)mutableData, CGImageSourceGetType(cgImage), 1, nil);
57-
CGImageDestinationAddImageFromSource(destination, cgImage, 0, (__bridge CFDictionaryRef)metaData);
52+
+ (NSData *)imageFromImage:(NSData *)imageData withMetaData:(NSDictionary *)metadata {
53+
NSMutableData *targetData = [NSMutableData data];
54+
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
55+
if (source == NULL) {
56+
return nil;
57+
}
58+
CGImageDestinationRef destination = NULL;
59+
CFStringRef sourceType = CGImageSourceGetType(source);
60+
if (sourceType != NULL) {
61+
destination =
62+
CGImageDestinationCreateWithData((__bridge CFMutableDataRef)targetData, sourceType, 1, nil);
63+
}
64+
if (destination == NULL) {
65+
CFRelease(source);
66+
return nil;
67+
}
68+
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metadata);
5869
CGImageDestinationFinalize(destination);
59-
CFRelease(cgImage);
70+
CFRelease(source);
6071
CFRelease(destination);
61-
return mutableData;
72+
return targetData;
6273
}
6374

6475
+ (NSData *)convertImage:(UIImage *)image

packages/image_picker/image_picker/ios/Classes/FLTImagePickerPhotoAssetUtil.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ + (NSString *)saveImageWithMetaData:(NSDictionary *)metaData
8686
usingType:type
8787
quality:imageQuality];
8888
if (metaData) {
89-
data = [FLTImagePickerMetaDataUtil updateMetaData:metaData toImage:data];
89+
NSData *updatedData = [FLTImagePickerMetaDataUtil imageFromImage:data withMetaData:metaData];
90+
// If updating the metadata fails, just save the original.
91+
if (updatedData) {
92+
data = updatedData;
93+
}
9094
}
9195

9296
return [self createFile:data suffix:suffix];

packages/image_picker/image_picker/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for selecting images from the Android and iOS image
33
library, and taking new pictures with the camera.
44
repository: https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
6-
version: 0.8.2
6+
version: 0.8.3
77

88
environment:
99
sdk: ">=2.12.0 <3.0.0"

0 commit comments

Comments
 (0)