在Flutter中drawImage或paintImage均无法正常工作 [英] Either drawImage or paintImage doesn't work properly in Flutter
问题描述
我基本上想将资产文件夹中的图像显示在画布上。
import'package:flutter /material.dart'作为ui;
...
ui.Image img = ui.Image.asset( images / some_image.png);
ui.paintImage(canvas:canvas,image:img);
当我尝试将img分配给paintImage的图像时,出现了以下错误消息。
参数类型'Image(C:\ABC\flutter\packages\flutter\lib\src\widgets\image .dart)'不能分配给参数类型'Image(C:\ABC\flutter\bin\cache\pkg\sky_engine\lib\ui\painting.dart)'。
我真的不知道会出什么问题,我已经看到其他与ui.Image有着类似方法的代码。 / p>
请告知。
有两个称为<$ c的类$ c>图像在不同包装中的变化。
有窗口小部件,其行为类似于窗口小部件,可以通过资产,内存或网络通过其命名的构造函数实例化。
还有ui包图片,该图片在较低级别(例如 CustomPainter
。由于此版本位于 ui
软件包中,因此通常使用 ui
前缀导入,如下所示:
import'dart:ui'作为ui;
不要将材料
导入为 ui
!
要制作小部件,请使用 Image.asset
构造函数,并通过
要从资产中制作 ui.Image
,请使用以下代码段:
Future< ui.Image> load(String asset)async {
ByteData data = await rootBundle.load(asset);
ui.Codec编解码器=等待ui.instantiateImageCodec(data.buffer.asUint8List());
ui.FrameInfo fi =等待codec.getNextFrame();
返回图片。
}
I basically want to show an image that I have in the assets folder onto the canvas.
import 'package:flutter/material.dart' as ui;
...
ui.Image img = ui.Image.asset("images/some_image.png");
ui.paintImage(canvas: canvas, image: img);
And have got the following error message when I tried to assign img to paintImage's image.
The argument type 'Image (C:\ABC\flutter\packages\flutter\lib\src\widgets\image.dart)' can't be assigned to the parameter type 'Image (C:\ABC\flutter\bin\cache\pkg\sky_engine\lib\ui\painting.dart)'.
I don't really know what could go wrong, I have seen other codes that had a similar approach with ui.Image.
Please advise.
There are two classes called Image
in flutter, in different packages.
There's the Widget, which behaves like a widget and can be instantiated from an asset, from memory or from the network through its named constructors.
There's also the ui package Image which is used when painting at a lower level, for example in a CustomPainter
. Since this version is in the ui
package it's normally imported with the ui
prefix like this:
import 'dart:ui' as ui;
Don't import material
as ui
! That will lead to a lot of confusion.
To make a widget, use the Image.asset
constructor, passing the asset name.
To make a ui.Image
from an asset use this snippet:
Future<ui.Image> load(String asset) async {
ByteData data = await rootBundle.load(asset);
ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List());
ui.FrameInfo fi = await codec.getNextFrame();
return fi.image;
}
这篇关于在Flutter中drawImage或paintImage均无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!