image size in flutter code example

Example 1: flutter network image size

Image.network(
          url,
          width: width,
          height: height,
          fit: BoxFit.cover,
        ));

Example 2: flutter image size percentage

MediaQuery.of(context).size.width * 1

Example 3: flutter image size percentage

Expanded(child: Image.asset("assets/mypic.jpg")),

Example 4: resize image flutter

//assets is List<Asset> from MultiImagePicker.pickImages
 assets.forEach((asset) {
      Future<ByteData> byteData = asset.getByteData();
      byteData.then((d) async {
        List<int> imageData = d.buffer.asUint8List();
        String b64 =base64Encode(imageData);
        print(b64); // prints [/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE...
        //if i send b64 to server then decode it and save as img it's working well

  //Resize
    ui.instantiateImageCodec(imageData,targetHeight: 800, targetWidth: 600)
        .then((codec) {
      codec.getNextFrame().then((frameInfo) async {
        ui.Image i = frameInfo.image;
        ByteData bytes = await i.toByteData();
        List<int> resizedImageData = bytes.buffer.asUint8List();
        String rb64 = base64Encode(resizedImageData);
        print(rb64); // prints too many backslashes:[k5KO/5qWk/+ZlZL/mpaT/5uXlP+alpP/mJSR/5iUkf+YlJH/mZSR/5uWk/+blpP/n5qX/6GcmP+gm5f/oZyY/6GcmP+fmpb/nZi..
        //If i send rb64 to server then server cannot decode and save it.
      });
    });
  });
});

Tags:

Misc Example