在Flutter中,如何确定图片的宽度和高度?

63
假设我已经在我的pubspec.yaml中声明了图片,像这样:

Assume I have declared my image in my pubspec.yaml like this:

  assets:
    - assets/kitten.jpg

我的Flutter代码如下:

void main() {
  runApp(
    new Center(
      child: new Image.asset('assets/kitten.jpg'),
    ),
  );
}

现在我有一个 new Image.asset(),我该如何确定该图像的宽度和高度?例如,我只想打印出图像的宽度和高度。

(看起来 dart:ui 的 Image 类 有宽度和高度,但不确定如何从小部件的 Image 转到 dart:ui 的 Image。)

谢谢!


你的解决方案完美地运行了 :) 谢谢 - Besart D. Laci
13个回答

0
此页面上的所有答案目前仅获取原始图像大小:即原始图像的像素高度和宽度。而不是Flutter应用程序中图像的大小。我需要调整边界框的大小,以便在调整页面大小时进行调整。我需要当前图像的大小,并且还需要知道何时更改,以便我可以调整边界框的大小。

GIF of app being resized with image and bounding box overlay resizing

我的解决方案涉及:

  • GlobalKeyaddPostFrameCallback来获取图像大小
  • 一些逻辑来在图像大小改变时进行渲染

在我的build方法顶部:

  final _imageKey = GlobalKey();
  Size imageSize = Size.zero;

  @override
  Widget build(BuildContext context) {
    MediaQuery.of(context); // Trigger rebuild when window is resized. This updates the bounding box sizes.
    final image = Image.asset(exampleImagePath, key: _imageKey);
    // Yes, we call this every time the widget rebuilds, so we update our understanding of the image size.
    WidgetsBinding.instance.addPostFrameCallback(_updateImageSize);

我的_updateImageSize

  void _updateImageSize(Duration _) {
    final size = _imageKey.currentContext?.size;
    if (size == null) return;
    if (imageSize != size) {
      imageSize = size;
      // When the window is resized using keyboard shortcuts (e.g. Rectangle.app),
      // The widget won't rebuild AFTER this callback. Therefore, the new
      // image size is not used to update the bounding box drawing. 
      // So we call setState
      setState(() {});
    }
  }

0
易得资产图像尺寸的方法。
import 'dart:ui' as ui;
import 'package:flutter/services.dart'; 

          Future<Size> _loadAssetImageSize(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 Size(fi.image.width.toDouble(), fi.image.height.toDouble());
      }

 

你从哪里得到这个“ui”?如果你只是写下这些东西,而不是强迫我们去其他地方找信息,那会帮助很多。 - Karolina Hagegård

0
这段代码异步加载图像资源并获取其宽度和高度。
此代码使用Image.asset()方法从“assets”目录加载名为“my_image.png”的图像资源。
创建Completer对象以处理图像的异步加载。当图像加载完成时,使用Completer完成future。
使用addListener()方法将ImageStreamListener添加到图像的图像流中。当图像完全加载时,将触发ImageStreamListener,并提供包含有关所加载图像信息的ImageInfo对象。然后使用所加载的ui.Image对象完成Completer。
最后,等待来自Completer的future以获取ui.Image对象。然后分别使用width和height属性获取ui.Image对象的宽度和高度。
Image image = Image.asset('assets/my_image.png');
Completer<ui.Image> completer = Completer<ui.Image>();
image.image.resolve(ImageConfiguration()).addListener(  
ImageStreamListener((ImageInfo info, bool _) {
    completer.complete(info.image); 
 }), 
);
ui.Image myImage = await completer.future; 
int width = myImage.width;
int height = myImage.height;

我已经修复了你的格式。请提供更多细节,说明这如何回答问题。 - mozway

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接