首页 > 解决方案 > 将应用程序中创建的图像(使用画家插件)保存到设备本地图库

问题描述

我尝试了几种方法,但我是 Flutter 和移动开发的新手,所以我不太熟悉整个环境。

我在这里使用了画家示例https://pub.dev/packages/painter/example

我使用此代码尝试保存图像文件。

image_file.dart

import 'dart:ui';

class ImageFile {
  var imageFile;

  get getImageFile => this.imageFile;

  set setImageFile(var imageFile) => this.imageFile = imageFile;

  ImageFile({required this.imageFile});
}

然后我创建了类的实例并分配了

Image.memory(snapshot.data)

作为

ImageFile(imageFile: Image.memory(snapshot.data!));

到它,以便我可以全局访问它,然后我创建了一个方法来保存 ImageFile 对象中的图像并将其附加到一个 floatingActionButton。

void _saveFileImage(path) async {
    String path = (await getApplicationDocumentsDirectory()).path;
    GallerySaver.saveImage(path);
  } 
floatingActionButton: FloatingActionButton(
          onPressed: () {
            if (imageFile!.getImageFile != null) {
              _saveFileImage(imageFile!.getImageFile);
            } else {
              debugPrint("null object");
            }
          }, //_saveImage(imageFile),
          tooltip: 'Save to gallery',
          child: const Icon(Icons.save_alt_outlined),
        ), 

如果您对如何使用画师包中的 gallery_saver 包或 image_gallery_saver 包有任何想法,请提供帮助。

image_file.dart - https://codeshare.io/Pdm0lY

main.dart - https://codeshare.io/78gMbL

谢谢

标签: flutterdartflutter-layoutflutter-dependenciescustom-painter

解决方案


推荐阅读