Flutter-图像在删除后仍然存在吗? [英] Flutter - Image remains after being deleted?

查看:87
本文介绍了Flutter-图像在删除后仍然存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在图像抖动中遇到一些奇怪的行为。

I am experiencing some weird behavior around images in flutter.

我正在使用相机包来捕获像这样的图像:

I am using the camera package to capture images like so:

void _takePicture() {
_controller.takePicture(_tempImagePath).then((_) {
  setState(() {
    _captured = true;
    _shutterButtonController.forward();
    _acceptCancelController.forward();
  });
});
}

非常简单-捕获的图像符合我的期望。如果您注意到 _captured 成员,我正在使用它来驱动摄像机预览或捕获的图像的显示。这是我的小部件树的代码段:

Pretty straightforward - the image is captured how I expect. If you notice the _captured member, I am using this to drive display of either the camera preview, or the captured image. Here is a snippet of my widget tree:

...
AspectRatio(
        aspectRatio: _controller.value.aspectRatio,
        child: _captured
            ? Image.file(File(_tempImagePath))
            : CameraPreview(_controller)),
...

这也和我预期的一样-捕获后显示图像。但是,要注意的是 Image.file(...)将始终显示相同的图像。我尝试了多种删除该图片的方法,但我相信它可以正常工作,但是该图片仍然存在。

This also behaves like I expect - the image is displayed after being captured. However, the catch appears to be that Image.file(...) will always display the same image. I have tried a number of ways to delete this image, and I believe it is working, yet the image somehow persists.

我现在要删除&在屏幕如下所示的情况下,在 initState()内重新创建整个目录:

I'm at the point where I am deleting & recreating the entire directory inside of initState() as the screen is constructed like so:

Future<String> get getTempImageDirectory async {
  final directory = await getApplicationDocumentsDirectory();
  var imageDirectory = Directory(directory.path + '/images/');

  print('The image directory ${imageDirectory.path} exists: ${imageDirectory.existsSync()}');

  if (imageDirectory.existsSync()) {
    imageDirectory.deleteSync(recursive: true);
  }

  imageDirectory.createSync();

  return imageDirectory.path;
}

即使这样,或按名称手动删除图像,原始捕获的图像仍保留在由 Image.file(...)创建的小部件。我相当确定删除操作正常-如果您不要删除它,那么相机API会抛出一个错误,即它无法覆盖以前的图像。

Even with this, or deleting the images manually by name, the originally captured image persists inside of the Widget created by Image.file(...). I am fairly certain that the delete is working fine - the camera API will throw an error that it cannot overwrite the previous image if you don't delete it.

最后,如果我终止了该应用程序并重新启动,则可以重新拍摄照片并看到一张新照片,但是同样,该照片将一直保留到整个应用程序被杀死为止。

Finally, if I kill the app and restart, I will be able to retake the picture and see a new one, but again, that one will remain until the whole application is killed. It seems like the image is somehow cached?

我对Flutter和Dart还是陌生的,所以可以提供任何帮助。

I'm fairly new to Flutter and Dart, so any help is appreciated.

推荐答案

搜索了Github问题后,我得知Flutter是将这些图像缓存在幕后。他们将保留在内存中,而不管从磁盘上删除什么。

After searching through Github issues, I was able to learn that Flutter is caching these images behind the scenes. They'll persist in-memory regardless of being deleted from disk.

我将其添加到删除方法中,问题得到解决。

I added this to my delete method, and my problem was solved.

import 'package:flutter/painting.dart';
...
imageCache.clear();

我也找到了答案此处,但是根据Flutter v0.5.1,导入语句似乎是错误的。

I've also found an answer here, however the import statement appears to be wrong based on Flutter v0.5.1.

这篇关于Flutter-图像在删除后仍然存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆