Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter flatter:如何使用url中的图像通过社交网络共享图像_Flutter_Share - Fatal编程技术网

Flutter flatter:如何使用url中的图像通过社交网络共享图像

Flutter flatter:如何使用url中的图像通过社交网络共享图像,flutter,share,Flutter,Share,在我的flatter应用程序中,我正在使用CachedNetworkImage显示来自互联网的图像 CachedNetworkImage( imageUrl: "http://via.placeholder.com/350x150", placeholder: (context, url) => CircularProgressIndicator(), errorWidget: (context, url, error) => Icon

在我的flatter应用程序中,我正在使用
CachedNetworkImage
显示来自互联网的图像

CachedNetworkImage(
        imageUrl: "http://via.placeholder.com/350x150",
        placeholder: (context, url) => CircularProgressIndicator(),
        errorWidget: (context, url, error) => Icon(Icons.error),
     ),
我现在希望能够与另一个应用程序共享缓存的图像,例如instagram。如何识别缓存的图像并将其作为share函数的参数传递。请参见下面的“我的代码”,在这里您可以看到我的共享函数使用
wcb共享
,它以
字节数据
作为源。我想用cachedImage替换assets/images/logo.png

FlatButton(
  onPressed: () {
    final ByteData bytes = await rootBundle.load('assets/images/logo.png');
    await WcFlutterShare.share(
        sharePopupTitle: 'share',
        fileName: 'share.png',
        mimeType: 'image/png',
        bytesOfFile: bytes.buffer.asUint8List());
  },
  child: Text(
    "Share ",
  ),
)
请注意,我目前的解决方案如下,但我下载图像2次

http.Response response = await http.get("http://via.placeholder.com/350x150");
final bytes = response.bodyBytes;

await WcFlutterShare.share(
sharePopupTitle: 'share',
fileName: 'share.jpeg',
mimeType: 'image/jpeg',
bytesOfFile: bytes);

您可以将自己的
cacheManager
传递给CachedNetworkImage,这将允许您使用
getFile
检索缓存文件。然后可以检索字节流

final BaseCacheManager baseCacheManager = DefaultCacheManager();

...

@override
Widget build(BuildContext context) {
  baseCacheManager
      .getFile("http://via.placeholder.com/350x150")
      .listen((info) {
    log(info.file.path);
    log(info.originalUrl);
  });

  return CachedNetworkImage(
    cacheManager: baseCacheManager,
    imageUrl: "http://via.placeholder.com/350x150",
    placeholder: (context, url) => CircularProgressIndicator(),
    errorWidget: (context, url, error) => Icon(Icons.error),
  );
}
这是我用来测试我的想法的按钮:

FlatButton(
  child: Text("Share"),
  onPressed: () {
    baseCacheManager
        .getFile("http://via.placeholder.com/350x150")
        .first
        .then((info) {
          info.file.readAsBytes().then((bytes) {
            WcFlutterShare.share(
              sharePopupTitle: 'share',
              fileName: 'share.jpeg',
              mimeType: 'image/jpeg',
              bytesOfFile: bytes);
            });
          });
}),

您可以将自己的
cacheManager
传递给CachedNetworkImage,这将允许您使用
getFile
检索缓存文件。然后可以检索字节流

final BaseCacheManager baseCacheManager = DefaultCacheManager();

...

@override
Widget build(BuildContext context) {
  baseCacheManager
      .getFile("http://via.placeholder.com/350x150")
      .listen((info) {
    log(info.file.path);
    log(info.originalUrl);
  });

  return CachedNetworkImage(
    cacheManager: baseCacheManager,
    imageUrl: "http://via.placeholder.com/350x150",
    placeholder: (context, url) => CircularProgressIndicator(),
    errorWidget: (context, url, error) => Icon(Icons.error),
  );
}
这是我用来测试我的想法的按钮:

FlatButton(
  child: Text("Share"),
  onPressed: () {
    baseCacheManager
        .getFile("http://via.placeholder.com/350x150")
        .first
        .then((info) {
          info.file.readAsBytes().then((bytes) {
            WcFlutterShare.share(
              sharePopupTitle: 'share',
              fileName: 'share.jpeg',
              mimeType: 'image/jpeg',
              bytesOfFile: bytes);
            });
          });
}),

谢谢,这是有道理的,但我猜它在我的代码中下载的图像是http请求的两倍,对吗?当你在缓存中获得图像时,你将从文件系统读取它,这样你就不会第二次下载:)谢谢,这是有道理的,但我猜它在我的代码中下载的图像是http请求的两倍,对吗?当您在缓存中获得映像时,您将从文件系统中读取它,这样您就不会再次下载它:)