From 3c0bed060b9fcd9d10ef905cf1b8e63040f7c953 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Wed, 31 Jul 2024 15:21:10 +0900 Subject: [PATCH] feat: Add on memory resize mode --- lib/src/taro.dart | 15 +++++++++++++-- lib/src/taro_resizer.dart | 2 +- lib/src/taro_type.dart | 17 ++++++++++------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/src/taro.dart b/lib/src/taro.dart index da9e8fd..6f4b761 100644 --- a/lib/src/taro.dart +++ b/lib/src/taro.dart @@ -1,5 +1,6 @@ import 'dart:typed_data'; +import 'package:flutter/painting.dart'; import 'package:taro/src/taro_image.dart'; import 'package:taro/src/taro_loader.dart'; import 'package:taro/src/taro_loader_network.dart'; @@ -78,20 +79,30 @@ class Taro { /// If [checkMaxAgeIfExist] is true, the method checks the max age of the data. /// [ifThrowMaxAgeHeaderError] is used to throw an exception if the max age header is invalid. /// The [resizeOption] parameter is used to resize the image. If it is not provided, the default resize option is used. - TaroImage loadImageProvider( + ImageProvider loadImageProvider( String url, { double scale = 1.0, Map headers = const {}, TaroResizeOption? resizeOption, TaroHeaderOption? headerOption, }) { - return TaroImage( + final image = TaroImage( url, scale: scale, resizeOption: resizeOption ?? _resizeOption, headers: headers, headerOption: headerOption ?? _headerOption, ); + + if (resizeOption?.mode == TaroResizeMode.memory) { + return ResizeImage.resizeIfNeeded( + resizeOption?.maxWidth, + resizeOption?.maxHeight, + image, + ); + } + + return image; } /// Loads the data from the provided URL and returns it as a byte array. diff --git a/lib/src/taro_resizer.dart b/lib/src/taro_resizer.dart index 48ce96b..19c1dc3 100644 --- a/lib/src/taro_resizer.dart +++ b/lib/src/taro_resizer.dart @@ -16,7 +16,7 @@ class TaroResizer { required String contentType, required TaroResizeOption resizeOption, }) async { - if (resizeOption.mode == TaroResizeMode.skip) { + if (resizeOption.mode case TaroResizeMode.skip || TaroResizeMode.memory) { // do nothing return ( bytes: bytes, diff --git a/lib/src/taro_type.dart b/lib/src/taro_type.dart index c92cb39..b8aee65 100644 --- a/lib/src/taro_type.dart +++ b/lib/src/taro_type.dart @@ -16,25 +16,28 @@ enum TaroResizeMode { /// The image is not resized. skip, - /// The image is resized to the original contentType. + /// The image is resized and saved original image. + memory, + + /// The image is resized to the original contentType and saved cache. original, - /// The image is resized to a gif. + /// The image is resized to a gif and saved cache. gif, - /// The image is resized to a jpg. + /// The image is resized to a jpg and saved cache. jpeg, - /// The image is resized to a png. + /// The image is resized to a png and saved cache. png, - /// The image is resized to a bmp. + /// The image is resized to a bmp and saved cache. bmp, - /// The image is resized to a ico. + /// The image is resized to a ico and saved cache. ico, - /// The image is resized to a tiff. + /// The image is resized to a tiff and saved cache. tiff, }