【问题标题】:PNG image background turns black after save保存后PNG图像背景变黑
【发布时间】:2021-06-29 23:40:55
【问题描述】:

Question 的潜在副本:尝试了解决方案但没有用

我正在开发一个 Flutter 中的图像着色应用程序,它允许用户为黑色轮廓图像着色并保存它。

现在我已经下载了相当多的 PNG 图片,这些图片看起来很正常,但一旦添加到颤振资产文件夹中,就会显示黑色平铺背景。

一旦添加到 图库 屏幕,用户可以选择他们想要的任何图像,应用程序会显示完全正常的图像。但是在我保存图像时为图像着色后,我看到黑色背景。

这是一个在 vs 中打开的代码

**这里的图像选择屏幕显示了一只完全正常的鸟:**

这是保存后的图片:

现在是编码部分。

这是我的显示图像部分:

Widget displayImage(BuildContext context, String providedImagePath) {
    return Container(
      // child: Container(
      //   decoration: BoxDecoration(
      //     color: Colors.white,
      //   ),
      child: Image(
        image: AssetImage(providedImagePath),
        // color: Color.fromRGBO(255, 255, 255, 1.0),
        colorBlendMode: BlendMode.modulate,
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
      ),
      //),
    );

注释掉的部分来自我尝试过但没有奏效的其他堆栈溢出答案。

这里是保存功能:

  Future<void> _save() async {
    RenderRepaintBoundary boundary =
        globalKey.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();

    //Request permissions if not already granted
    if (!(await Permission.storage.status.isGranted))
      await Permission.storage.request();

    final result = await ImageGallerySaver.saveImage(
        Uint8List.fromList(pngBytes),
        quality: 60,
        name: "henlo");
    print(result);
  }

【问题讨论】:

    标签: image flutter dart png


    【解决方案1】:

    可能介于两者之间

    boundary.toImage();
    

    byteData.buffer.asUnit8List();
    

    我不确定是什么导致了这个问题,但你可以再次移除背景,试试这个解决方法https://gist.github.com/plateaukao/ebb5e7169dd89cc52bda338762d4997e

    【讨论】:

      猜你喜欢
      • 2011-10-31
      • 2012-01-09
      • 2014-10-05
      • 2018-03-15
      • 1970-01-01
      • 2021-09-14
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多