Skip to content

[IOS]Image.toByteData() is super slow on Flutter 3 #106773

@ssyzh

Description

@ssyzh
await image.toByteData(format: ImageByteFormat.png);

image.toByteData time consuming in flutter 2.10.5 and flutter 3.0.3, only in iOS

Steps to Reproduce

  1. Execute flutter run on the code sample
  2. in •flutter 2.10.5, • Dart version 2.16.2 • DevTools version 2.9.2 , total time is 1219ms
  3. in •flutter 3.0.3, • Dart version 2.17.5 • DevTools version 2.12.2 , total time is 8942ms

Expected results: The total time should be about the same

Actual results: 847ms and 8942ms, nearly eight times the difference! Even the real machine in release runs very very slow,eight times the difference!.

Code sample
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey sharePageKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: RepaintBoundary(
          key: sharePageKey,
          child: Column(
            children: [
              Text(List.generate(200, (index) => "Text").join("#")),
              Image.network(
                "http://pic1.win4000.com/wallpaper/2020-10-10/5f811214aa09e.jpg",
                width: double.infinity,
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          createImage();
        },
        child: const Text("create"),
      ),
    );
  }

  createImage() async {
    debugPrint("tim0 ${DateTime.now()}");
    DateTime start = DateTime.now();
    BuildContext? buildContext = sharePageKey.currentContext;
    if (null != buildContext) {
      RenderRepaintBoundary boundary =
          buildContext.findRenderObject() as RenderRepaintBoundary;
      ui.Image image = await boundary.toImage(pixelRatio: 5);
      debugPrint("tim1 ${DateTime.now()}");

      ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
      debugPrint("tim2  ${DateTime.now()}");

      if (byteData != null) {
        var pngBytes = byteData.buffer.asUint8List();
        debugPrint("tim3 ${DateTime.now()}");
        debugPrint(
            "total ${DateTime.now().millisecondsSinceEpoch - start.millisecondsSinceEpoch}");
      }
    }
  }
}
Logs

flutter: tim0 2022-06-29 10:58:55.542207
flutter: tim1 2022-06-29 10:58:55.621826
flutter: tim2  2022-06-29 10:58:56.764545
flutter: tim3 2022-06-29 10:58:56.764890
flutter: total 1219


[✓] Flutter (Channel unknown, 2.10.5, on macOS 12.3.1 21E258 darwin-x64, locale zh-Hans-CN)
    • Flutter version 2.10.5 at /Users/sunhao/Documents/developer/flutter
    • Upstream repository unknown
    • Framework revision 5464c5bac7 (2 months ago), 2022-04-18 09:55:37 -0700
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2
    • Pub download mirror https://mirrors.tuna.tsinghua.edu.cn/dart-pub
    • Flutter download mirror https://mirrors.tuna.tsinghua.edu.cn/flutter

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at /Users/sunhao/Documents/developer/Android/sdk
    • Platform android-32, build-tools 32.0.0
    • ANDROID_HOME = /Users/sunhao/Documents/developer/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • 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.12+0-b1504.28-7817840)

[✓] VS Code (version 1.68.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
    • iPhone 12 (mobile) • 9A3514D1-1F7A-4114-BEFF-9609739BE58C • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-4 (simulator)
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome 103.0.5060.53
    ! Error: ssyzh-iPhone is not connected. Xcode will continue when ssyzh-iPhone is connected. (code -13)

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
flutter: tim0 2022-06-29 10:37:19.759706
flutter: tim1 2022-06-29 10:37:19.865037
flutter: tim2  2022-06-29 10:37:28.702424
flutter: tim3 2022-06-29 10:37:28.703828
flutter: total 8942


[✓] Flutter (Channel stable, 3.0.3, on macOS 12.3.1 21E258 darwin-x64, locale zh-Hans-CN)
    • Flutter version 3.0.3 at /Users/sunhao/Documents/developer/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 676cefaaff (6 days ago), 2022-06-22 11:34:49 -0700
    • Engine revision ffe7b86a1e
    • Dart version 2.17.5
    • DevTools version 2.12.2
    • Pub download mirror https://mirrors.tuna.tsinghua.edu.cn/dart-pub
    • Flutter download mirror https://mirrors.tuna.tsinghua.edu.cn/flutter

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at /Users/sunhao/Documents/developer/Android/sdk
    • Platform android-32, build-tools 32.0.0
    • ANDROID_HOME = /Users/sunhao/Documents/developer/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • 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.12+0-b1504.28-7817840)

[✓] VS Code (version 1.68.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (4 available)
    • ssyzh-iPhone (mobile) • 00008020-000371162EC1002E            • ios            • iOS 15.2.1 19C63
    • iPhone 12 (mobile)    • 9A3514D1-1F7A-4114-BEFF-9609739BE58C • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-4 (simulator)
    • macOS (desktop)       • macos                                • darwin-x64     • macOS 12.3.1 21E258 darwin-x64
    • Chrome (web)          • chrome                               • web-javascript • Google Chrome 103.0.5060.53

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
Process finished with exit code 0


Metadata

Metadata

Assignees

No one assigned

    Labels

    a: imagesLoading, displaying, rendering imagesc: performanceRelates to speed or footprint issues (see "perf:" labels)c: regressionIt was better in the past than it is nowengineflutter/engine related. See also e: labels.found in release: 3.0Found to occur in 3.0found in release: 3.1Found to occur in 3.1has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-iosiOS applications specificallyr: duplicateIssue is closed as a duplicate of an existing issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions