无法在Flutter图像小部件中打开大尺寸(500MB)图像。

3
我正在使用Flutter开发一个图像查看器桌面应用程序。我尝试使用Flutter Image.file('largeImage.jpg')打开图像,但是它无法加载图像并显示以下错误:
 ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞═════════════════════════════════════  
    The following _Exception was thrown resolving an image frame:
    Exception: Codec failed to produce an image, possibly due to invalid image data.
    
    When the exception was thrown, this was the stack
    
    Path: images/largeImage.jpg

Image

当我在任何其他内置图像查看器中打开同一张图片时,它会打开。在Flutter中,如果使用相同的代码Image.file('smallSize.jpg'),则可以正常工作。

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text('Image Viewer'),
      ),
      body: Center(
        child:
            //not working
            Image.file(File('images/largeImage.JPG')),//515MB
            
            //working
            Image.file(File('images/smallImage.JPG')), //10MB

      ),
    );
  }

我也尝试使用extended_image库,但它没有显示任何内容(只是一个空白窗口),也没有异常。

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text('Image Viewer'),
      ),
      body: Center(
        child:
            
          ExtendedImage.file(
           width: 600,
           height: 400,
           File('images/Panel.JPG'),

          //cancelToken: cancellationToken,
         ),
      ),
    );
  }

以下是flutter.doctor的摘要:
Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.3.9, on Microsoft Windows [Version
10.0.19041.1415], locale en-US) [√] Android toolchain - develop for Android devices (Android SDK version 32.0.0) [√] Chrome - develop for the web [√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.4.2) [√] Android Studio (version 2020.3) [√] VS Code (version 1.74.0) [√] Connected device (3 available) [√] HTTP Host Availability

• No issues found!

非常感谢您的提前帮助


2
你能分享这张图片让我们测试一下吗? - MendelG
@MendelG 很抱歉,根据我们公司的保密政策,我无法分享这张图片。这些图片是以JPG格式保存,大小为515MB,分辨率为30000x30000像素(基本上是AOI设备图像)。 - Pabdev
1个回答

2

你能否测试以下代码,老实说我不确定这会不会有效。

Image.file(File(path),
            width: 200,
            height: 200,
            cacheHeight: 200,
            cacheWidth: 200,
          )

阅读这篇有用的评论


使用cacheHeight和cacheWidth完美地解决了问题。现在图片正在加载。非常感谢您。似乎是由于高分辨率的图像尺寸较大,因此需要更多的内存。 - Pabdev

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