ImageInfo adds a new getter named sizeBytes to decouple ImageCache and ui.Image (#86555)

This commit is contained in:
ColdPaleLight 2021-08-19 00:21:12 +08:00 committed by GitHub
parent df399f9a8d
commit 33f5ac6650
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 1 deletions

View file

@ -411,7 +411,7 @@ class ImageCache {
void listener(ImageInfo? info, bool syncCall) { void listener(ImageInfo? info, bool syncCall) {
int? sizeBytes; int? sizeBytes;
if (info != null) { if (info != null) {
sizeBytes = info.image.height * info.image.width * 4; sizeBytes = info.sizeBytes;
info.dispose(); info.dispose();
} }
final _CachedImage image = _CachedImage( final _CachedImage image = _CachedImage(

View file

@ -98,6 +98,9 @@ class ImageInfo {
/// the image. /// the image.
final ui.Image image; final ui.Image image;
/// The size of raw image pixels in bytes.
int get sizeBytes => image.height * image.width * 4;
/// The linear scale factor for drawing this image at its intended size. /// The linear scale factor for drawing this image at its intended size.
/// ///
/// The scale factor applies to the width and the height. /// The scale factor applies to the width and the height.

View file

@ -0,0 +1,26 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' as ui show Image;
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';
Future<void> main() async {
final ui.Image smallImage = await createTestImage(width: 10, height: 20);
final ui.Image middleImage = await createTestImage(width: 20, height: 100);
final ui.Image bigImage = await createTestImage(width: 100, height: 200);
test('ImageInfo sizeBytes', () {
ImageInfo imageInfo = ImageInfo(image: smallImage);
expect(imageInfo.sizeBytes, equals(800));
imageInfo = ImageInfo(image: middleImage);
expect(imageInfo.sizeBytes, equals(8000));
imageInfo = ImageInfo(image: bigImage);
expect(imageInfo.sizeBytes, equals(80000));
});
}

View file

@ -43,6 +43,9 @@ class TestImageInfo implements ImageInfo {
image.dispose(); image.dispose();
} }
@override
int get sizeBytes => image.height * image.width * 4;
@override @override
int get hashCode => hashValues(value, image, scale, debugLabel); int get hashCode => hashValues(value, image, scale, debugLabel);