fix: 修复macos保存图片

This commit is contained in:
speakeloudest 2026-01-25 00:10:08 -08:00
parent e59cbf8b60
commit 2fab52d362
2 changed files with 47 additions and 18 deletions

View File

@ -1,6 +1,7 @@
import 'dart:io'; import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'dart:ui' as ui; import 'dart:ui' as ui;
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -15,35 +16,44 @@ class HIAntiLostController extends GetxController {
Future<void> saveImage() async { Future<void> saveImage() async {
// KRCommonUtil.kr_showLoading(message: "正在保存..."); // KRCommonUtil.kr_showLoading(message: "正在保存...");
try { try {
// Load image from assets
final ByteData data =
await rootBundle.load('assets/images/lost-share-image.png');
final Uint8List pngBytes = data.buffer.asUint8List();
if (Platform.isMacOS || Platform.isWindows || Platform.isLinux) {
// Desktop: Use FilePicker to save file
String? outputFile = await FilePicker.platform.saveFile(
dialogTitle: '保存图片',
fileName: 'Hi快联系方式',
type: FileType.custom,
allowedExtensions: ['png'],
);
if (outputFile != null) {
final file = File(outputFile);
await file.writeAsBytes(pngBytes);
KRCommonUtil.kr_showToast("已保存至: $outputFile");
}
} else {
// Mobile: Use Gal to save to gallery
// Check permission // Check permission
final hasAccess = await Gal.hasAccess(); final hasAccess = await Gal.hasAccess();
if (!hasAccess) { if (!hasAccess) {
await Gal.requestAccess(); await Gal.requestAccess();
} }
// Load image from assets
final ByteData data =
await rootBundle.load('assets/images/lost-share-image.png');
final Uint8List pngBytes = data.buffer.asUint8List();
await Gal.putImageBytes(pngBytes); await Gal.putImageBytes(pngBytes);
String message = "已保存至系统相册";
if (Platform.isMacOS) {
message = "已保存至系统【照片】应用";
} else if (Platform.isWindows) {
message = "已保存至系统【图片】文件夹";
}
HIDialog.show( HIDialog.show(
message: message, message: "已保存至系统相册",
); );
}
} catch (e) { } catch (e) {
debugPrint("Save image error: $e"); debugPrint("Save image error: $e");
if (e is GalException) { if (e is GalException) {
KRCommonUtil.kr_showToast("保存失败: No Permission"); KRCommonUtil.kr_showToast("保存失败: No Permission");
} else { } else {
KRCommonUtil.kr_showToast("保存失败"); KRCommonUtil.kr_showToast("保存失败: $e");
} }
} finally { } finally {
//KRCommonUtil.kr_hideLoading(); //KRCommonUtil.kr_hideLoading();

View File

@ -4,9 +4,13 @@ PODS:
- ReachabilitySwift - ReachabilitySwift
- device_info_plus (0.0.1): - device_info_plus (0.0.1):
- FlutterMacOS - FlutterMacOS
- file_picker (0.0.1):
- FlutterMacOS
- flutter_inappwebview_macos (0.0.1): - flutter_inappwebview_macos (0.0.1):
- FlutterMacOS - FlutterMacOS
- OrderedSet (~> 6.0.3) - OrderedSet (~> 6.0.3)
- flutter_secure_storage_macos (6.1.3):
- FlutterMacOS
- flutter_udid (0.0.1): - flutter_udid (0.0.1):
- FlutterMacOS - FlutterMacOS
- SAMKeychain - SAMKeychain
@ -33,13 +37,18 @@ PODS:
- FlutterMacOS - FlutterMacOS
- url_launcher_macos (0.0.1): - url_launcher_macos (0.0.1):
- FlutterMacOS - FlutterMacOS
- webview_flutter_wkwebview (0.0.1):
- Flutter
- FlutterMacOS
- window_manager (0.2.0): - window_manager (0.2.0):
- FlutterMacOS - FlutterMacOS
DEPENDENCIES: DEPENDENCIES:
- connectivity_plus (from `Flutter/ephemeral/.symlinks/plugins/connectivity_plus/macos`) - connectivity_plus (from `Flutter/ephemeral/.symlinks/plugins/connectivity_plus/macos`)
- device_info_plus (from `Flutter/ephemeral/.symlinks/plugins/device_info_plus/macos`) - device_info_plus (from `Flutter/ephemeral/.symlinks/plugins/device_info_plus/macos`)
- file_picker (from `Flutter/ephemeral/.symlinks/plugins/file_picker/macos`)
- flutter_inappwebview_macos (from `Flutter/ephemeral/.symlinks/plugins/flutter_inappwebview_macos/macos`) - flutter_inappwebview_macos (from `Flutter/ephemeral/.symlinks/plugins/flutter_inappwebview_macos/macos`)
- flutter_secure_storage_macos (from `Flutter/ephemeral/.symlinks/plugins/flutter_secure_storage_macos/macos`)
- flutter_udid (from `Flutter/ephemeral/.symlinks/plugins/flutter_udid/macos`) - flutter_udid (from `Flutter/ephemeral/.symlinks/plugins/flutter_udid/macos`)
- FlutterMacOS (from `Flutter/ephemeral`) - FlutterMacOS (from `Flutter/ephemeral`)
- gal (from `Flutter/ephemeral/.symlinks/plugins/gal/darwin`) - gal (from `Flutter/ephemeral/.symlinks/plugins/gal/darwin`)
@ -50,6 +59,7 @@ DEPENDENCIES:
- share_plus (from `Flutter/ephemeral/.symlinks/plugins/share_plus/macos`) - share_plus (from `Flutter/ephemeral/.symlinks/plugins/share_plus/macos`)
- tray_manager (from `Flutter/ephemeral/.symlinks/plugins/tray_manager/macos`) - tray_manager (from `Flutter/ephemeral/.symlinks/plugins/tray_manager/macos`)
- url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`) - url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)
- webview_flutter_wkwebview (from `Flutter/ephemeral/.symlinks/plugins/webview_flutter_wkwebview/darwin`)
- window_manager (from `Flutter/ephemeral/.symlinks/plugins/window_manager/macos`) - window_manager (from `Flutter/ephemeral/.symlinks/plugins/window_manager/macos`)
SPEC REPOS: SPEC REPOS:
@ -63,8 +73,12 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/connectivity_plus/macos :path: Flutter/ephemeral/.symlinks/plugins/connectivity_plus/macos
device_info_plus: device_info_plus:
:path: Flutter/ephemeral/.symlinks/plugins/device_info_plus/macos :path: Flutter/ephemeral/.symlinks/plugins/device_info_plus/macos
file_picker:
:path: Flutter/ephemeral/.symlinks/plugins/file_picker/macos
flutter_inappwebview_macos: flutter_inappwebview_macos:
:path: Flutter/ephemeral/.symlinks/plugins/flutter_inappwebview_macos/macos :path: Flutter/ephemeral/.symlinks/plugins/flutter_inappwebview_macos/macos
flutter_secure_storage_macos:
:path: Flutter/ephemeral/.symlinks/plugins/flutter_secure_storage_macos/macos
flutter_udid: flutter_udid:
:path: Flutter/ephemeral/.symlinks/plugins/flutter_udid/macos :path: Flutter/ephemeral/.symlinks/plugins/flutter_udid/macos
FlutterMacOS: FlutterMacOS:
@ -85,15 +99,19 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/tray_manager/macos :path: Flutter/ephemeral/.symlinks/plugins/tray_manager/macos
url_launcher_macos: url_launcher_macos:
:path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos :path: Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos
webview_flutter_wkwebview:
:path: Flutter/ephemeral/.symlinks/plugins/webview_flutter_wkwebview/darwin
window_manager: window_manager:
:path: Flutter/ephemeral/.symlinks/plugins/window_manager/macos :path: Flutter/ephemeral/.symlinks/plugins/window_manager/macos
SPEC CHECKSUMS: SPEC CHECKSUMS:
connectivity_plus: 18d3c32514c886e046de60e9c13895109866c747 connectivity_plus: 18d3c32514c886e046de60e9c13895109866c747
device_info_plus: 1b14eed9bf95428983aed283a8d51cce3d8c4215 device_info_plus: 1b14eed9bf95428983aed283a8d51cce3d8c4215
file_picker: e716a70a9fe5fd9e09ebc922d7541464289443af
flutter_inappwebview_macos: bdf207b8f4ebd58e86ae06cd96b147de99a67c9b flutter_inappwebview_macos: bdf207b8f4ebd58e86ae06cd96b147de99a67c9b
flutter_secure_storage_macos: c2754d3483d20bb207bb9e5a14f1b8e771abcdb9
flutter_udid: 2e7b3da4b5fdfba86a396b97898f5fe8f4ec1a52 flutter_udid: 2e7b3da4b5fdfba86a396b97898f5fe8f4ec1a52
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24 FlutterMacOS: d0db08ddef1a9af05a5ec4b724367152bb0500b1
gal: 6a522c75909f1244732d4596d11d6a2f86ff37a5 gal: 6a522c75909f1244732d4596d11d6a2f86ff37a5
in_app_purchase_storekit: a1ce04056e23eecc666b086040239da7619cd783 in_app_purchase_storekit: a1ce04056e23eecc666b086040239da7619cd783
OrderedSet: e539b66b644ff081c73a262d24ad552a69be3a94 OrderedSet: e539b66b644ff081c73a262d24ad552a69be3a94
@ -105,6 +123,7 @@ SPEC CHECKSUMS:
share_plus: 76dd39142738f7a68dd57b05093b5e8193f220f7 share_plus: 76dd39142738f7a68dd57b05093b5e8193f220f7
tray_manager: 9064e219c56d75c476e46b9a21182087930baf90 tray_manager: 9064e219c56d75c476e46b9a21182087930baf90
url_launcher_macos: c82c93949963e55b228a30115bd219499a6fe404 url_launcher_macos: c82c93949963e55b228a30115bd219499a6fe404
webview_flutter_wkwebview: 29eb20d43355b48fe7d07113835b9128f84e3af4
window_manager: 3a1844359a6295ab1e47659b1a777e36773cd6e8 window_manager: 3a1844359a6295ab1e47659b1a777e36773cd6e8
PODFILE CHECKSUM: 3100e00928a3ab9aaf1f0d7f435c5ff0d92c057c PODFILE CHECKSUM: 3100e00928a3ab9aaf1f0d7f435c5ff0d92c057c