Merge pull request #7 from DieLustigenTierwesen/copilot/sub-pr-6
Offload image compression to background isolate
This commit is contained in:
commit
f470b0d9a7
1 changed files with 6 additions and 0 deletions
|
|
@ -113,6 +113,12 @@ class StorageService {
|
||||||
|
|
||||||
static Future<Uint8List> _compressImage(File source) async {
|
static Future<Uint8List> _compressImage(File source) async {
|
||||||
final bytes = await source.readAsBytes();
|
final bytes = await source.readAsBytes();
|
||||||
|
return compute(_compressImageBytes, bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Performs CPU-intensive image decoding, resizing, and JPEG encoding.
|
||||||
|
/// Runs in a background isolate via [compute] to avoid blocking the UI thread.
|
||||||
|
static Uint8List _compressImageBytes(Uint8List bytes) {
|
||||||
final decoded = img.decodeImage(bytes);
|
final decoded = img.decodeImage(bytes);
|
||||||
if (decoded == null) {
|
if (decoded == null) {
|
||||||
throw Exception('Invalid image file.');
|
throw Exception('Invalid image file.');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue