app直播原始碼,flutter 生成圖片並儲存到相簿
阿新 • • 發佈:2022-05-17
app直播原始碼,flutter 生成圖片並儲存到相簿
1、在pubspec.yaml新增依賴
# 圖片儲存
image_gallery_saver: ^1.7.1
獲取外掛
flutter pub get
2、引入標頭檔案
import 'dart:io';
import 'dart:typed_data';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:ui' as ui;
3、程式碼實現
GlobalKey repaintKey = GlobalKey();
Widget showQrCodeView() {
return Container(
child: Center(
child: RepaintBoundary(
key: repaintKey,
child: Container(
width: 200.px,
height: 230.px,
color: Colors.blue,
)),
),
);
}
void saveQrcodeImage() {
Alert.showLoading();
RenderRepaintBoundary boundary =
repaintKey.currentContext.findRenderObject();
boundary.toImage(pixelRatio: SizeUtil.pixelRatio).then((value) async {
ByteData byteData =
await value.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
Permission filePermission =
AppEnv.isIos ? Permission.photos : Permission.storage;
var status = await filePermission.status;
if (!status.isGranted) {
Map<Permission, PermissionStatus> statuses =
await [filePermission].request();
saveQrcodeImage();
}
if (status.isGranted) {
final result = await ImageGallerySaver.saveImage(pngBytes, quality: 80);
print(result.toString());
if (result["isSuccess"]) {
Alert.showSuccess(message: "儲存成功");
print('圖片儲存 ok');
// toast("儲存成功", wring: false);
} else {
print('圖片儲存 error');
// toast("儲存失敗");
}
}
if (status.isDenied) {
print("拒絕訪問照片檔案");
}
});
}
以上就是 app直播原始碼,flutter 生成圖片並儲存到相簿,更多內容歡迎關注之後的文章