Flutter入门疑难杂症:wechat_kit 获取网络图片调用微信分享
程序员文章站
2022-03-28 18:24:13
在用wechat kit分享的时候,遇到分享网页,但是图片需要使用自己服务端的图片链接,且图片可能大于32*1024的问题。解决方案:先下载图片,再进行压缩。贴代码: //加载网络图片 static Future compressWebImage( int targetSize, String imgUrl) async { Uint8List bytes; if (imgUrl != null) { try {...
在用wechat kit分享的时候,遇到分享网页,但是图片需要使用自己服务端的图片链接,且图片可能大于32*1024的问题。
解决方案:
先下载图片,再进行压缩。贴代码:
//加载网络图片
static Future<Uint8List> compressWebImage(
int targetSize, String imgUrl) async {
Uint8List bytes;
if (imgUrl != null) {
try {
HttpClient _httpClient = HttpClient();
Uri resolved = Uri.base.resolve(imgUrl);
HttpClientRequest request = await _httpClient.getUrl(resolved);
HttpClientResponse response = await request.close();
if (response.statusCode == HttpStatus.ok) {
bytes = await MediaUtils.compressImgWithInt(
30 * 1024, await consolidateHttpClientResponseBytes(response));
}
} catch (e) {
bytes = null;
print(e);
}
}
return bytes;
}
//调整大小
static Future<List<int>> compressImgWithInt(
int targetSize, Uint8List bytes) async {
List<int> resizeBytes = await FlutterImageCompress.compressWithList(bytes,
minHeight: 200, minWidth: 200, quality: 40);
if (Uint8List.fromList(resizeBytes).length > targetSize) {
resizeBytes = await FlutterImageCompress.compressWithList(bytes,
minHeight: 200, minWidth: 200, quality: 20);
}
return Uint8List.fromList(resizeBytes);
}
主要是下载图片,并进行压缩到32*1024那部分的代码。
最后调用分享:
_wechat.shareWebpage(
scene: scene,
description: des,
title: title,
webpageUrl: targetUrl,
thumbData: bytes);
分享集成的话自己去看下 wechat kit 吧。
本文地址:https://blog.csdn.net/WZAHD/article/details/108578508
下一篇: JavaScript数组去重方法总结