flutter/flutter

canvas cannot draw image

Closed this issue · 3 comments

related #23621 and seems like not fixed yet.

Steps to Reproduce

canvas can not draw image in release mode.

  1. copy code to main.dart
import 'package:flutter/material.dart';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

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

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

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  ByteData byteData;

  @override
  void initState() {
    super.initState();

    this._renderImage();
  }

  Future<ui.Image> _loadImage(String url) async {
    final _image = await http.readBytes(url);

    final bg = await ui.instantiateImageCodec(_image);
    final frame = await bg.getNextFrame();
    final img = frame.image;
    return img;
  }

  _renderImage() async {
    final recorder = new ui.PictureRecorder();
    final canvas = new Canvas(
        recorder,
        Rect.fromPoints(
            Offset(0.0, 0.0),
            Offset(200, 200)
        )
    );
    final img = await this._loadImage('https://ws1.sinaimg.cn/large/8112eefdgy1g08jm47mh2j20u01hc4dq.jpg');
    canvas.drawImage(img, Offset.zero, Paint());
    final picture = recorder.endRecording();
    final png = await picture.toImage(200, 200).toByteData(format: ui.ImageByteFormat.png);

    setState(() {
      byteData = png;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: byteData == null ? Placeholder() : Image.memory(this.byteData.buffer.asUint8List())
      )
    );
  }
}
  1. open app

run flutter by command flutter run --enable-software-rendering and flutter run --release

the image has shown in software rendering and empty in release mode

  1. device MI 6

software rendering:

image

gpu rendering:

image

flutter doctor

❯ flutter doctor -v
[√] Flutter (Channel beta, v1.1.8, on Microsoft Windows [Version 10.0.17763.316], locale en-US)
    • Flutter version 1.1.8 at D:\flutter\flutter
    • Framework revision 985ccb6d14 (6 weeks ago), 2019-01-08 13:45:55 -0800
    • Engine revision 7112b72cc2
    • Dart version 2.1.1 (build 2.1.1-dev.0.1 ec86471ccc)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at D:\AndroidSDK
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = D:\AndroidSDK
    • Java binary at: D:\Program Files\Android\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[√] Android Studio (version 3.3)
    • Android Studio at D:\Program Files\Android
    • Flutter plugin version 32.0.1
    • Dart plugin version 182.5124
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[!] IntelliJ IDEA Ultimate Edition (version 2017.2)
    • IntelliJ at D:\Program Files\JetBrains\IntelliJ IDEA 2017.2
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.io/intellij-setup/#installing-the-plugins

[!] IntelliJ IDEA Ultimate Edition (version 2018.1)
    • IntelliJ at D:\Program Files\JetBrains\IntelliJ IDEA 2018.1
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.io/intellij-setup/#installing-the-plugins

[√] VS Code, 64-bit edition (version 1.31.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 2.23.0

[√] Connected device (1 available)
    • MI 4LTE • 9532401b • android-arm • Android 6.0.1 (API 23)

! Doctor found issues in 2 categories.

Please check with master instead of Channel beta, v1.1.8

Flutter 1.2.2-pre.21 • channel beta • https://github.com/flutter/flutter.git
Framework • revision 035e0765cc (32 hours ago) • 2019-02-16 01:48:28 -0500
Engine • revision f37b09a11b
Tools • Dart 2.1.2 (build 2.1.2-dev.0.0 c92d5ca288)

has fixed.

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 flutter doctor -v and a minimal reproduction of the issue.