-
Notifications
You must be signed in to change notification settings - Fork 28.5k
ui.Image.toByteData can't use the default rawRgba format if it's later used for Image.memory #37423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The app could provide an implementation of Example:
|
Thanks! Considering its frequent uses, I wonder if it's worth to add a new constructor |
seems like something we could put in a package |
Using the code listed here https://gist.github.com/liyuqian/560c3edefe2bce147147a49db0ec4e98 and running on latest stable using Android device and by removing the png encoding throws below exception:
Selecting flutter doctor -v
|
Running provided sample code on the latest Flutter channels and seeing that the exception persists when using default LogsE/FlutterJNI(24007): Failed to decode image
E/FlutterJNI(24007): android.graphics.ImageDecoder$DecodeException: Failed to create image decoder with message 'unimplemented'Input contained an error.
E/FlutterJNI(24007): at android.graphics.ImageDecoder.nCreate(Native Method)
E/FlutterJNI(24007): at android.graphics.ImageDecoder.access$200(ImageDecoder.java:172)
E/FlutterJNI(24007): at android.graphics.ImageDecoder$ByteBufferSource.createImageDecoder(ImageDecoder.java:242)
E/FlutterJNI(24007): at android.graphics.ImageDecoder.decodeBitmapImpl(ImageDecoder.java:2015)
E/FlutterJNI(24007): at android.graphics.ImageDecoder.decodeBitmap(ImageDecoder.java:2008)
E/FlutterJNI(24007): at io.flutter.embedding.engine.FlutterJNI.decodeImage(FlutterJNI.java:524)
══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
The following _Exception was thrown resolving an image codec:
Exception: Invalid image data
When the exception was thrown, this was the stack:
#0 _futurize (dart:ui/painting.dart:5886:5)
#1 ImageDescriptor.encoded (dart:ui/painting.dart:5741:12)
#2 instantiateImageCodecFromBuffer (dart:ui/painting.dart:2092:60)
#3 PaintingBinding.instantiateImageCodecFromBuffer (package:flutter/src/painting/binding.dart:153:15)
#4 MemoryImage._loadAsync (package:flutter/src/painting/image_provider.dart:1090:20)
<asynchronous suspension>
════════════════════════════════════════════════════════════════════════════════════════════════════
Sample code (be compatible with the new flutter version)import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Stack(
fit: StackFit.expand,
children: <Widget>[
CachedFrostedBox(
opaqueBackground: Container(
color: Colors.white,
child: Text('0' * 10000),
),
sigmaX: 2.0,
sigmaY: 2.0,
child: Container(
alignment: Alignment.center,
child: Text('Hello'),
),
),
Center(
child: Padding(
padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
child: LinearProgressIndicator(),
),
),
],
),
),
showPerformanceOverlay: true,
);
}
}
class Foo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Stack(
fit: StackFit.expand,
children: <Widget>[
CachedFrostedBox(
opaqueBackground: Container(
color: Colors.white,
child: Text('0' * 10000),
),
sigmaX: 2.0,
sigmaY: 2.0,
child: Container(
alignment: Alignment.center,
child: Text('Hello'),
),
),
Center(
child: Padding(
padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
child: LinearProgressIndicator(),
),
),
],
);
}
}
class CachedFrostedBox extends StatefulWidget {
CachedFrostedBox({required this.child, this.sigmaX = 8, this.sigmaY = 8, required this.opaqueBackground})
: this.frostBackground = Stack(
children: <Widget>[
opaqueBackground,
ClipRect(
child: BackdropFilter(
filter: ui.ImageFilter.blur(sigmaX: sigmaX, sigmaY: sigmaY),
child: new Container(
decoration: new BoxDecoration(
color: Colors.white.withOpacity(0.1),
)
),
)
),
],
);
final Widget child;
final double sigmaY;
final double sigmaX;
/// This must be opaque so the backdrop filter won't access any colors beneath this background.
final Widget opaqueBackground;
/// Blur applied to the opaqueBackground. See the constructor.
final Widget frostBackground;
@override
State<StatefulWidget> createState() {
return CachedFrostedBoxState();
}
}
class CachedFrostedBoxState extends State<CachedFrostedBox> {
final GlobalKey _snapshotKey = GlobalKey();
Image? _backgroundSnapshot;
bool _snapshotLoaded = false;
bool _skipSnapshot = false;
void _snapshot(Duration _) async {
final RenderRepaintBoundary renderBackground = _snapshotKey.currentContext?.findRenderObject() as RenderRepaintBoundary;
final ui.Image image = await renderBackground.toImage(
pixelRatio: WidgetsBinding.instance.window.devicePixelRatio,
);
// !!! The default encoding rawRgba will throw exceptions. This bug is introducing a lot
// of encoding/decoding work.
final ByteData? imageByteData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
setState(() {
_backgroundSnapshot = Image.memory(imageByteData!.buffer.asUint8List());
});
}
@override
Widget build(BuildContext context) {
Widget frostedBackground;
if (_backgroundSnapshot == null || _skipSnapshot) {
frostedBackground = RepaintBoundary(
key: _snapshotKey,
child: widget.frostBackground,
);
if (!_skipSnapshot) {
SchedulerBinding.instance.addPostFrameCallback(_snapshot);
}
} else {
// !!! We don't seem to have a way to know when IO thread
// decoded the image.
if (!_snapshotLoaded) {
frostedBackground = widget.frostBackground;
Future.delayed(Duration(seconds: 1), () {
setState(() {
_snapshotLoaded = true;
});
});
} else {
frostedBackground = Offstage();
}
}
return Stack(
children: <Widget>[
frostedBackground,
if (_backgroundSnapshot != null) _backgroundSnapshot!,
widget.child,
GestureDetector(
onTap: () {
setState(() { _skipSnapshot = !_skipSnapshot; });
}
),
],
);
}
} flutter doctor -v (stable and master)[✓] Flutter (Channel stable, 3.3.9, on macOS 13.0 22A380 darwin-x64, locale en-VN)
• Flutter version 3.3.9 on channel stable at /Users/huynq/Documents/GitHub/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision b8f7f1f986 (31 hours ago), 2022-11-23 06:43:51 +0900
• Engine revision 8f2221fbef
• Dart version 2.18.5
• DevTools version 2.15.0
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at /Users/huynq/Library/Android/sdk
• Platform android-33, build-tools 31.0.0
• ANDROID_HOME = /Users/huynq/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14A400
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
[✓] IntelliJ IDEA Community Edition (version 2022.2.2)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin version 70.0.5
• Dart plugin version 222.4167.21
[✓] IntelliJ IDEA Community Edition (version 2022.1.1)
• IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
[✓] VS Code (version 1.73.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.52.0
[✓] Connected device (3 available)
• SM T225 (mobile) • R9JT3004VRJ • android-arm64 • Android 12 (API 31)
• macOS (desktop) • macos • darwin-x64 • macOS 13.0 22A380 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.110
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
[!] Flutter (Channel master, 3.7.0-9.0.pre.11, on macOS 13.0.1 22A400 darwin-x64, locale en-VN)
• Flutter version 3.7.0-9.0.pre.11 on channel master at /Users/huynq/Documents/GitHub/flutter_master
! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision dbc9306380 (4 hours ago), 2022-12-14 13:53:20 -0800
• Engine revision 0a6a4a58f4
• Dart version 3.0.0 (build 3.0.0-21.0.dev)
• DevTools version 2.20.0
• If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at /Users/huynq/Library/Android/sdk
• Platform android-33, build-tools 31.0.0
• ANDROID_HOME = /Users/huynq/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14B47b
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
[✓] IntelliJ IDEA Community Edition (version 2022.1.1)
• IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
[✓] VS Code (version 1.73.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.54.0
[✓] Connected device (4 available)
• SM T225 (mobile) • R9JT3004VRJ • android-arm64 • Android 12 (API 31)
• iPhone (mobile) • d9a94afe2b649fef56ba0bfeb052f0f2a7dae95e • ios • iOS 15.7.2 19H218
• macOS (desktop) • macos • darwin-x64 • macOS 13.0.1 22A400 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 108.0.5359.98
! Error: iPhone is busy: Fetching debug symbols for iPhone. Xcode will continue when iPhone is finished. (code -10)
[✓] HTTP Host Availability
• All required HTTP hosts are available
! Doctor found issues in 1 category.
|
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
To reproduce, run the app in https://gist.github.com/liyuqian/560c3edefe2bce147147a49db0ec4e98 and remove its PNG encoding specification (search for
!!!
).My Flutter version is c8be195 when this issue is reproduced locally. This issue may have been there for a while. For example, cl/260167188 was using PNG because of this.
The text was updated successfully, but these errors were encountered: