Skip to content

Commit 6c4835a

Browse files
committed
remove need for package:image to shrink package size
1 parent b1e9807 commit 6c4835a

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import 'dart:async';
12
import 'dart:math';
2-
import 'dart:typed_data';
3+
import 'dart:ui' as ui;
34

5+
import 'package:flutter/widgets.dart';
46
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
5-
import 'package:image/image.dart';
67

78
const supportedFileNames = ['jpg', 'jpeg', 'png', 'tga', 'gif', 'cur', 'ico'];
89
mixin ImageCacheManager on BaseCacheManager {
@@ -24,8 +25,7 @@ mixin ImageCacheManager on BaseCacheManager {
2425
int? maxWidth,
2526
}) async* {
2627
if (maxHeight == null && maxWidth == null) {
27-
yield* getFileStream(url,
28-
key: key, headers: headers, withProgress: withProgress);
28+
yield* getFileStream(url, key: key, headers: headers, withProgress: withProgress);
2929
return;
3030
}
3131
key ??= url;
@@ -60,6 +60,7 @@ mixin ImageCacheManager on BaseCacheManager {
6060
}
6161

6262
final Map<String, Stream<FileResponse>> _runningResizes = {};
63+
6364
Future<FileInfo> _resizeImageFile(
6465
FileInfo originalFile,
6566
String key,
@@ -72,7 +73,7 @@ mixin ImageCacheManager on BaseCacheManager {
7273
return originalFile;
7374
}
7475

75-
var image = decodeImage(await originalFile.file.readAsBytes())!;
76+
var image = await _decodeImage(originalFile.file);
7677
if (maxWidth != null && maxHeight != null) {
7778
var resizeFactorWidth = image.width / maxWidth;
7879
var resizeFactorHeight = image.height / maxHeight;
@@ -82,13 +83,13 @@ mixin ImageCacheManager on BaseCacheManager {
8283
maxHeight = (image.height / resizeFactor).round();
8384
}
8485

85-
var resized = copyResize(image, width: maxWidth, height: maxHeight);
86-
var resizedFile = encodeNamedImage(resized, originalFileName)!;
86+
var resized = await _decodeImage(originalFile.file, width: maxWidth, height: maxHeight, allowUpscaling: true);
87+
var resizedFile = (await resized.toByteData(format: ui.ImageByteFormat.png))!.buffer.asUint8List();
8788
var maxAge = originalFile.validTill.difference(DateTime.now());
8889

8990
var file = await putFile(
9091
originalFile.originalUrl,
91-
Uint8List.fromList(resizedFile),
92+
resizedFile,
9293
key: key,
9394
maxAge: maxAge,
9495
fileExtension: fileExtension,
@@ -131,3 +132,13 @@ mixin ImageCacheManager on BaseCacheManager {
131132
}
132133
}
133134
}
135+
136+
Future<ui.Image> _decodeImage(File file, {int? width, int? height, bool allowUpscaling = false}) {
137+
// since we set allowUpscaling to false, the width and height parameters are actually max-width.
138+
final image = ResizeImage(FileImage(file), width: width, height: height, allowUpscaling: allowUpscaling);
139+
final completer = Completer<ui.Image>();
140+
image
141+
.resolve(const ImageConfiguration())
142+
.addListener(ImageStreamListener((info, _) => completer.complete(info.image)));
143+
return completer.future;
144+
}

flutter_cache_manager/pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ dependencies:
1313
flutter:
1414
sdk: flutter
1515
http: ^0.13.0
16-
image: ^3.0.1
1716
path: ^1.8.0
1817
path_provider: ^2.0.0
1918
pedantic: ^1.10.0

0 commit comments

Comments
 (0)