1+ import 'dart:async' ;
12import 'dart:math' ;
2- import 'dart:typed_data' ;
3+ import 'dart:ui' as ui ;
34
5+ import 'package:flutter/widgets.dart' ;
46import 'package:flutter_cache_manager/flutter_cache_manager.dart' ;
5- import 'package:image/image.dart' ;
67
78const supportedFileNames = ['jpg' , 'jpeg' , 'png' , 'tga' , 'gif' , 'cur' , 'ico' ];
89mixin 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+ }
0 commit comments