假设我在 pubspec.yaml 中声明了我的图像,如下所示:
assets:
- assets/kitten.jpg
我的 Flutter 代码是这样的:
void main() {
runApp(
new Center(
child: new Image.asset('assets/kitten.jpg'),
),
);
}
现在我有一个 new Image.asset()
,如何确定该图像的宽度和高度?比如我只想打印出图片的宽高。
(看起来 dart:ui's Image class 有宽度和高度,但不知道如何从小部件的图像转到 dart:ui 的图像。)
谢谢!
最佳答案
如果您只想要异步函数中图像的宽度和高度,其他答案似乎过于复杂。您可以像这样直接使用flutter lib获取图像分辨率:
import 'dart:io';
File image = new File('image.png'); // Or any other way to get a File instance.
var decodedImage = await decodeImageFromList(image.readAsBytesSync());
print(decodedImage.width);
print(decodedImage.height);
https://stackoverflow.com/questions/44665955/