Flutter将网络映像保存到本地目录 [英] Flutter save a network image to local directory

查看:474
本文介绍了Flutter将网络映像保存到本地目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter中,如何将网络中的图像保存到本地目录中.

我是编码和解码图像的新手.谁能指出我正确的方向?

解决方案

对此有一个简单的答案和一个更复杂的答案.我将描述复杂的一个,然后再给您简单的一个.

一个复杂的答案是,您可以使用NetworkImage手动对其进行解析,解析并获取图像流.拥有图像流后,您可以使用flutter的文件读取&将其保存到文件系统中.写作能力(是了解更多有关此的好资源)-您还需要使用一个名为PathProvider的插件,该插件可为该链接中介绍的iOS和Android获取正确的路径.您还希望跟踪所有已下载的图像,并可能在一定时间后将其删除.您还必须先读回文件,然后才能使用它们创建Image小部件.

这给了您很多控制权,但是却需要一些工作(尽管不是一个疯狂的数目,但是如果您是新手,那么根据您想要保存图像的原因,也许您现在不想做的事情)

简单的答案是救援包!另一个人已经遇到了这个问题,并编写了一个可以为您解决问题的插件,因此您不必考虑它!

有关插件的信息,请参见 cached_network_image包.

您需要添加到pubspec.yaml

中的依赖项

dependencies:
  cached_network_image: "^0.3.0"

导入:

import 'package:cached_network_image/cached_network_image.dart';

并使用它!

new CachedNetworkImage(
   imageUrl: "http://imageurl.png",
   placeholder: new CircularProgressIndicator(),
   errorWidget: new Icon(Icons.error),
),

请注意,这会下载&显示图像-如果要单独执行这些操作,则可以使用new CachedNetworkImageProvider(url)并使用new Image进行显示.

In Flutter how to save an image from network to the local directory.

I am new to encoding and decoding images. Can anyone point me in the right direction?

解决方案

There's a simple answer and a more complicated answer to this. I'll describe the complicated one and then give you the simple one.

The complicated answer is that you could manually cache images by using a NetworkImage, resolving it, and getting the image stream. Once you have the image stream, you could save it to the filesystem using flutter's file reading & writing capabilities (this is a good resource to learn more about that) - you also need to use a plugin called PathProvider which gets the right path for both iOS and Android which is described in that link. You'd also want to keep track of all the images you'd downloaded and probably delete them after certain amount of time. You'd also have to read the files back before using them to create Image widgets.

That gives you lots of control, but is a bit of work (although not a crazy amount, but if you're new to flutter maybe not something you want to do just now depending on why you want to save the images).

The simple answer is Packages to the rescue! Someone else has already come across this problem and written a plugin that solves it for you, so you don't have to think about it!

See cached_network_image package for information about the plugin.

You need to add to your dependencies in pubspec.yaml

dependencies:
  cached_network_image: "^0.3.0"

Import it:

import 'package:cached_network_image/cached_network_image.dart';

And use it!

new CachedNetworkImage(
   imageUrl: "http://imageurl.png",
   placeholder: new CircularProgressIndicator(),
   errorWidget: new Icon(Icons.error),
),

Note that this downloads & shows the image - if you want to do those seperately you can use a new CachedNetworkImageProvider(url) and show it using new Image.

这篇关于Flutter将网络映像保存到本地目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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