flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.14k stars 27.47k forks source link

ResizeImage cannot resize gif #90804

Open weiminghuaa opened 3 years ago

weiminghuaa commented 3 years ago

CachedNetworkImage has memCacheWidth/memCacheHeight to reduce image memory, I think it implement by ResizeImage when look CachedNetworkImage src. But it's not work in gif, how to solve it?

ResizeImage.resizeIfNeeded(
          cacheWidth,
          cacheHeight,
          image,
        ),
weiminghuaa commented 3 years ago

24835

darshankawar commented 3 years ago

@weiminghuaa Are you referring / using https://pub.dev/packages/cached_network_image/install plugin ?

weiminghuaa commented 3 years ago

@darshankawar Yes. I mean, I use the CachedNetworkImage memCacheWidth to reduce the memory usage of very large images. This method is effective for many image formats, such as webp and png, but it is not valid for gif. I see that CachedNetworkImage memCacheWidth is actually call ResizeImage resizeIfNeeded, so I want to ask if the problem lies in ResizeImage

darshankawar commented 3 years ago

@weiminghuaa What platform are you trying this on ? Is it possible to provide a self contained code only using ReSizeImage with a sample gif, that we can directly use to verify this ?

weiminghuaa commented 3 years ago

@darshankawar I have tested memory only using ReSizeImage with a sample gif. You can test the following code, no matter which way, the memory usage is 36.5M

截屏2021-09-28 21 07 18
class SampleItemDetailsView extends StatelessWidget {
  const SampleItemDetailsView({Key? key}) : super(key: key);

  static const routeName = '/sample_item';

  @override
  Widget build(BuildContext context) {
    // origin
    return Scaffold(
        appBar: AppBar(
          title: const Text('Item Details'),
        ),
        body: Center(
            child: Image.network(
          "https://d1hzxpwavt5kus.cloudfront.net/home/16322955386612_74ec4a82-bd04-4e91-93ed-5594e1778cc2.gif",
        )));
    // resize image
    return Scaffold(
        appBar: AppBar(
          title: const Text('Item Details'),
        ),
        body: Center(
            child: Image(
                image: ResizeImage.resizeIfNeeded(
                    375, // test code, set small to test memory
                    null,
                    const NetworkImage(
                      "https://d1hzxpwavt5kus.cloudfront.net/home/16322955386612_74ec4a82-bd04-4e91-93ed-5594e1778cc2.gif",
                    )))));
    // CachedNetworkImage
    return Scaffold(
      appBar: AppBar(
        title: const Text('Item Details'),
      ),
      body: Center(
        child: CachedNetworkImage(
          imageUrl:
              "https://d1hzxpwavt5kus.cloudfront.net/home/16322955386612_74ec4a82-bd04-4e91-93ed-5594e1778cc2.gif", // width: MediaQuery.of(context).size.width,
          width: MediaQuery.of(context).size.width,
          memCacheWidth: (MediaQuery.of(context).size.width *
                  MediaQuery.of(context).devicePixelRatio)
              .toInt(),
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}
darshankawar commented 3 years ago

body: Center( child: Image( image: ResizeImage.resizeIfNeeded( 375, // test code, set small to test memory null, const NetworkImage( "https://d1hzxpwavt5kus.cloudfront.net/home/16322955386612_74ec4a82-bd04-4e91-93ed-5594e1778cc2.gif", )))));

@weiminghuaa If we consider above code snippet, since it uses core components and not 3rd party plugin, I see that above, you have provided only cacheWidth and kept cacheHeight as null.

Looking at the documentation, it states Composes the provider in a ResizeImage only when cacheWidth and cacheHeight are not both null.. So can you try by providing both parameters and see if it helps ?

https://api.flutter.dev/flutter/painting/ResizeImage/resizeIfNeeded.html

weiminghuaa commented 3 years ago

I have try by providing both width and height, it's not work.

darshankawar commented 3 years ago

@weiminghuaa How are you measuring the memory ? Can you compare it with a sample image (png or jpeg, basically non-gif image) and see if it resizes and reflects in the memory you are measuring ?

weiminghuaa commented 3 years ago

I use Dart DevTools measure the memory. There is no problem with png or jpeg, but gif.

darshankawar commented 3 years ago

Thanks for the update. I see the same behavior on latest stable and master.

stable, master flutter doctor -v ``` [✓] Flutter (Channel stable, 2.5.2, on Mac OS X 10.15.4 19E2269 darwin-x64, locale en-GB) • Flutter version 2.5.2 at /Users/dhs/documents/fluttersdk/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 3595343e20 (5 days ago), 2021-09-30 12:58:18 -0700 • Engine revision 6ac856380f • Dart version 2.14.3 [✓] Android toolchain - develop for Android devices (Android SDK version 30) • Android SDK at /Users/dhs/Library/Android/sdk • Platform android-30, build-tools 30.0.3 • ANDROID_HOME = /Users/dhs/Library/Android/sdk • Java binary at: /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.3, Build version 12C33 • CocoaPods version 1.10.1 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 4.1) • Android Studio at /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) [✓] VS Code (version 1.57.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (4 available) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 93.0.4577.82 • No issues found! [✓] Flutter (Channel master, 2.6.0-12.0.pre.268, on Mac OS X 10.15.4 19E2269 darwin-x64, locale en-GB) • Flutter version 2.6.0-12.0.pre.268 at /Users/dhs/documents/fluttersdk/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision b9d2177da0 (29 minutes ago), 2021-10-07 21:11:07 -0700 • Engine revision 8d64613c9b • Dart version 2.15.0 (build 2.15.0-178.0.dev) [✓] Android toolchain - develop for Android devices (Android SDK version 30) • Android SDK at /Users/dhs/Library/Android/sdk • Platform android-30, build-tools 30.0.3 • ANDROID_HOME = /Users/dhs/Library/Android/sdk • Java binary at: /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.5.1, Build version 12E507 • CocoaPods version 1.10.1 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 4.1) • Android Studio at /Users/dhs/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/202.7486908/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) [✓] VS Code (version 1.57.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [[✓] Connected device (4 available) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 93.0.4577.82 • No issues found! ```
snailflying commented 2 months ago

Oh,it is since 2021,Now is 2024! Is there any progress?