Flutter:如何使用来自URL的图像通过社交网络共享图像

问题描述 投票:0回答:1

在我的flutter应用程序中,我正在使用CachedNetworkImage从互联网显示图像。

CachedNetworkImage(
        imageUrl: "http://via.placeholder.com/350x150",
        placeholder: (context, url) => CircularProgressIndicator(),
        errorWidget: (context, url, error) => Icon(Icons.error),
     ),

我现在希望能够与另一个应用程序(例如instagram)共享该缓存的图像。如何识别缓存的图像并将其作为共享函数的参数传递。在下面的代码中,您可以看到使用WcFlutterShare作为源的ByteData共享功能。我想用cachedImage替换asset / 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);
flutter share
1个回答
0
投票

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

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);
            });
          });
}),
© www.soinside.com 2019 - 2024. All rights reserved.