Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance on WEB #8

Closed
leonardojribeiro opened this issue Feb 16, 2022 · 1 comment
Closed

Performance on WEB #8

leonardojribeiro opened this issue Feb 16, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@leonardojribeiro
Copy link

leonardojribeiro commented Feb 16, 2022

Hi!
I found a possibility of most performance on flutter web.
Its possible to use the resources of the dart:ui package.
In example bellow you can resize an image and possible pass the width and heigth properties for the image in result.

Size _calculateSize({required Size imageSize, double? width, double? height}) {
    if (width != null && height != null) {
      return Size(width, height);
    } else if (width != null) {
      final scale = width / imageSize.width;
      final height = imageSize.height * scale;
      return Size(width, height);
    } else if (height != null) {
      final scale = height / imageSize.height;
      final width = imageSize.width * scale;
      return Size(width, height);
    }
    return imageSize;
  }
  
Future<ui.Image> resize(Uint8List image, {double? width, double? height}) async {
    final codec = await ui.instantiateImageCodec(image);
    final frameInfo = await codec.getNextFrame();
    final pictureRecorder = ui.PictureRecorder();
    final size = _calculateSize(
      imageSize: Size(
        frameInfo.image.width.toDouble(),
        frameInfo.image.height.toDouble(),
      ),
      height: height,
      width: width,
    );
    Canvas(pictureRecorder).drawImageRect(
      frameInfo.image,
      Rect.fromLTWH(0, 0, frameInfo.image.width.toDouble(), frameInfo.image.height.toDouble()), //change here 
      Rect.fromLTWH(0, 0, size.width, size.height), //change here
      Paint(),
    );
    frameInfo.image.dispose();
    return await pictureRecorder.endRecording().toImage(size.width.toInt(), size.height.toInt());
  }

For croping, you maybe change the parameters commenteds such necessary.
I hope this helps.

@sahilmind sahilmind added the enhancement New feature or request label Jul 8, 2022
@deep-mindinventory
Copy link

@leonardojribeiro are you checking with our latest version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants