【问题标题】:In flutter, using PictureRecorder to draw picture will cause the memory to be unreleasedFlutter中使用PictureRecorder画图会导致内存未释放
【发布时间】:2021-11-11 10:03:42
【问题描述】:

我使用 Canvas 对图像进行了一些特殊的绘制,然后保存。 当我调用picture.toImage 时,内存不会回落。 好像picutre.toImage已经缓存了图片,可以释放这个缓存吗?

  void _getImage() async {
    final imageFile = await _picker.getImage(source: ImageSource.gallery);
    if (imageFile == null) {
      return;
    }
    Uint8List bytes = await imageFile.readAsBytes();
    final Completer<ui.Image> completer = Completer();
    ui.decodeImageFromList(bytes, (ui.Image img) {
      return completer.complete(img);
    });
    ui.Image image = await completer.future;

    Size size = Size(image.width.toDouble(), image.height.toDouble());
    final recorder = ui.PictureRecorder();
    final canvas = Canvas(
        recorder,
        Rect.fromLTWH(
            0, 0, image.width.toDouble(), image.height.toDouble()));
    canvas.drawImageRect(
        image,
        ui.Rect.fromLTWH(
            0, 0, image.width.toDouble(), image.height.toDouble()),
        ui.Rect.fromLTWH(
            0, 0, image.width.toDouble(), image.height.toDouble()),
        Paint());

    image.dispose();
    ui.Picture picture = recorder.endRecording();
    final resutImage =
        await picture.toImage(size.width.toInt(), size.height.toInt());
    picture.dispose();
    resutImage.dispose();
  }

【问题讨论】:

    标签: image flutter canvas memory


    【解决方案1】:

    dart:ui 图片可以保留 SkImage 对象,导致 GPU 内存使用量增加 https://github.com/flutter/flutter/issues/28912

    试试 dispose()

    【讨论】:

      猜你喜欢
      • 2018-09-30
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 2020-03-14
      相关资源
      最近更新 更多