Skip to content

Commit 0851c39

Browse files
authored
[image_picker] Fix scaling 10-bit images (flutter#12557)
Resizing a 10-bit wide-gamut UIImage can carry its source renderer format into UIGraphicsImageRenderer. On iOS 26 this can request an unsupported 10-bit integer bitmap context, producing a zero-size image and an empty XFile after JPEG conversion. This change renders scaled images with a standard-range format while preserving the source scale and opacity. The native regression test synthesizes a Display-P3 kCIFormatRGB10 image and verifies that the same 1920-pixel and JPEG-quality path produces a non-empty, decodable image. Fixes flutter#185623. ## Tests - dart run script/tool/bin/flutter_plugin_tools.dart format --packages=image_picker_ios - dart run script/tool/bin/flutter_plugin_tools.dart analyze --packages=image_picker_ios - dart run script/tool/bin/flutter_plugin_tools.dart dart-test --packages=image_picker_ios - dart run script/tool/bin/flutter_plugin_tools.dart native-test --ios --no-integration --packages=image_picker_ios - Focused native regression test verified failing before the production change and passing afterward on an iPhone 17 simulator running iOS 26. ## Pre-Review Checklist
1 parent 13b5214 commit 0851c39

4 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎packages/image_picker/image_picker_ios/CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 0.8.13+8
22

3+
* Fixes scaling 10-bit images on iOS.
34
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
45

56
## 0.8.13+7

‎packages/image_picker/image_picker_ios/example/ios/RunnerTests/ImageUtilTests.m‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#if __has_include(<image_picker_ios/image_picker_ios-umbrella.h>)
99
@import image_picker_ios.Test;
1010
#endif
11+
@import CoreImage;
1112
@import UniformTypeIdentifiers;
1213
@import XCTest;
1314

@@ -260,4 +261,46 @@ - (void)testScaledGIFImage_UsesClampedDelayWhenUnclampedDelayMissing API_AVAILAB
260261
XCTAssertEqualWithAccuracy(info.interval, 0.25, 0.001);
261262
}
262263

264+
- (void)testScaledImage_10BitWideGamutImageCanBeEncodedAsJPEG {
265+
if (@available(iOS 17.0, *)) {
266+
CGColorSpaceRef displayP3ColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3);
267+
CIColor *color = [CIColor colorWithRed:0.5
268+
green:0.25
269+
blue:0.75
270+
alpha:1.0
271+
colorSpace:displayP3ColorSpace];
272+
CIImage *ciImage =
273+
[[CIImage imageWithColor:color] imageByCroppingToRect:CGRectMake(0, 0, 2880, 2160)];
274+
CIContext *ciContext = [CIContext contextWithOptions:nil];
275+
CGImageRef cgImage = [ciContext createCGImage:ciImage
276+
fromRect:ciImage.extent
277+
format:kCIFormatRGB10
278+
colorSpace:displayP3ColorSpace];
279+
XCTAssertNotEqual(cgImage, nil);
280+
if (cgImage == nil) {
281+
CGColorSpaceRelease(displayP3ColorSpace);
282+
return;
283+
}
284+
XCTAssertEqual(CGImageGetBitsPerComponent(cgImage), 10);
285+
286+
UIImage *image = [UIImage imageWithCGImage:cgImage];
287+
CGImageRelease(cgImage);
288+
CGColorSpaceRelease(displayP3ColorSpace);
289+
290+
UIImage *scaledImage = [FLTImagePickerImageUtil scaledImage:image
291+
maxWidth:@1920
292+
maxHeight:@1920
293+
isMetadataAvailable:YES];
294+
XCTAssertEqual(scaledImage.size.width, 1920);
295+
XCTAssertEqual(scaledImage.size.height, 1440);
296+
297+
NSData *encodedData = [FLTImagePickerMetaDataUtil convertImage:scaledImage
298+
usingType:FLTImagePickerMIMETypeJPEG
299+
quality:@0.8];
300+
XCTAssertNotNil(encodedData);
301+
XCTAssertGreaterThan(encodedData.length, 0U);
302+
XCTAssertNotNil([UIImage imageWithData:encodedData]);
303+
}
304+
}
305+
263306
@end

‎packages/image_picker/image_picker_ios/ios/image_picker_ios/Sources/image_picker_ios/FLTImagePickerImageUtil.m‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ @implementation FLTImagePickerImageUtil : NSObject
3333
if (imageToScale == nil || width == 0 || height == 0) {
3434
return nil;
3535
}
36+
// The source format can request an unsupported 10-bit integer bitmap context,
37+
// producing an empty scaled image. Use a fresh standard-range format while
38+
// preserving the source scale and opacity.
39+
UIGraphicsImageRendererFormat *imageRendererFormat = [[UIGraphicsImageRendererFormat alloc] init];
40+
imageRendererFormat.scale = imageToScale.imageRendererFormat.scale;
41+
imageRendererFormat.opaque = imageToScale.imageRendererFormat.opaque;
42+
imageRendererFormat.preferredRange = UIGraphicsImageRendererFormatRangeStandard;
3643
UIGraphicsImageRenderer *imageRenderer =
3744
[[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(width, height)
38-
format:imageToScale.imageRendererFormat];
45+
format:imageRendererFormat];
3946
return [imageRenderer imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) {
4047
CGContextRef cgContext = rendererContext.CGContext;
4148

‎packages/image_picker/image_picker_ios/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: image_picker_ios
22
description: iOS implementation of the image_picker plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker_ios
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
5-
version: 0.8.13+7
5+
version: 0.8.13+8
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)