Skip to content

Commit 54706bd

Browse files
authored
[image_picker_ios] Return an error for undecodable image data (flutter#12267)
`PHPicker` can provide data for a type that conforms to `UTTypeImage` even when `UIImage` cannot decode the bytes. The linked issue reports this with an AVIF selection. The existing path continues with a `nil` image and can treat the selection as successfully saved. This change completes the operation with the existing `invalid_image` error when decoding fails. It also adds a regression test that verifies the callback returns no path and that the `NSOperation` finishes. Fixes flutter#132154 No public API documentation changes are needed because this uses the existing image-picker error contract. Tests run: - `flutter_plugin_tools format --packages image_picker_ios --fail-on-change` - `flutter_plugin_tools analyze --ios --packages image_picker_ios` - `flutter_plugin_tools dart-test --packages image_picker_ios` - `flutter_plugin_tools native-test --ios --no-integration --packages image_picker_ios` - `flutter_plugin_tools validate --packages image_picker_ios` - `flutter_plugin_tools license-check` - `(from packages/image_picker/image_picker_ios) dart pub publish --dry-run` ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent b3570ac commit 54706bd

5 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎packages/image_picker/image_picker_ios/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.13+10
2+
3+
* Returns an error when selected image data cannot be decoded.
4+
15
## 0.8.13+9
26

37
* Adds injectable protocol seams for camera, photo library, and PHPicker APIs.

‎packages/image_picker/image_picker_ios/example/ios/RunnerTests/PickerSaveImageToPathOperationTests.swift‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ struct PickerSaveImageToPathOperationTests {
147147
#expect(savedError?.details as? String == "PHPickerDomain")
148148
}
149149

150+
@Test func undecodableImageDataReturnsError() async throws {
151+
let itemProvider = StaticDataItemProvider(data: Data("not an image".utf8))
152+
let result = FakePickerItem(itemProvider: itemProvider, assetIdentifier: nil)
153+
let (savedPath, savedError) = try await runSaveOperation(result: result, fullMetadata: true)
154+
#expect(savedPath == nil)
155+
#expect(savedError?.code == "invalid_image")
156+
#expect(savedError?.message == "Could not decode image data.")
157+
}
158+
150159
@Test func initWithNilResultReturnsNil() {
151160
let operation = FLTPHPickerSaveImageToPathOperation(
152161
result: nil,

‎packages/image_picker/image_picker_ios/example/ios/RunnerTests/TestDoubles.swift‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@ final class FailingDataItemProvider: NSItemProvider {
4444
}
4545
}
4646

47+
/// NSItemProvider that reports image conformance and loads the given data.
48+
final class StaticDataItemProvider: NSItemProvider {
49+
let data: Data
50+
51+
init(data: Data) {
52+
self.data = data
53+
super.init()
54+
}
55+
56+
@available(*, unavailable)
57+
required init?(coder: NSCoder) {
58+
fatalError("init(coder:) is not supported")
59+
}
60+
61+
override func hasItemConformingToTypeIdentifier(_ typeIdentifier: String) -> Bool {
62+
true
63+
}
64+
65+
override func loadDataRepresentation(
66+
forTypeIdentifier typeIdentifier: String,
67+
completionHandler: @escaping @Sendable (Data?, (any Error)?) -> Void
68+
) -> Progress {
69+
completionHandler(data, nil)
70+
return Progress()
71+
}
72+
}
73+
4774
/// NSItemProvider that reports movie conformance and optionally loads a file URL.
4875
final class MovieItemProvider: NSItemProvider {
4976
let movieURL: URL?

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ - (void)start {
127127
/// Processes the image.
128128
- (void)processImage:(NSData *)pickerImageData API_AVAILABLE(ios(14)) {
129129
UIImage *localImage = [[UIImage alloc] initWithData:pickerImageData];
130+
if (localImage == nil) {
131+
FlutterError *flutterError = [FlutterError errorWithCode:@"invalid_image"
132+
message:@"Could not decode image data."
133+
details:nil];
134+
[self completeOperationWithPath:nil error:flutterError];
135+
return;
136+
}
130137

131138
if (self.maxWidth != nil || self.maxHeight != nil) {
132139
localImage = [FLTImagePickerImageUtil scaledImage:localImage

‎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+9
5+
version: 0.8.13+10
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)