You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Maintain aspect ratio when changing the cache size of an image. #52802
flutter version: Flutter 1.12.13+hotfix.8
A widget Image width:100 height:100
A picture width:1000 height:1500
display the picture into the Image : Image.file(new File(path) , fit: BoxFit.cover, cacheWidth: 100, cacheHeight: 100)
the displayed picture is distorted with size:100*100
if I modify as below: Image.file(new File(path) , fit: BoxFit.cover, cacheWidth: 100)
the displayed picture with size:100*150
Proposal
In 1 the picture is distorted , 2 is waste memory
I wanted is the picture center clip and keep aspect like BoxFit.cover
String _instantiateImageCodec(Uint8List list,
_Callback<Codec> callback,
_ImageInfo imageInfo,
int targetWidth, int targetHeight)
native 'instantiateImageCodec';
If codec with a params like Image.fit(BoxFit.cover, BoxFit.fill), the codec image can fit like widget Image, What you see is what you get, is the best.
Reacted by Britannio Jarrett, Rostyslav Lesovyi and Samuel
changed the title [-]_instantiateImageCodec optimization wanted[/-][+]Maintain aspect ratio when changing the cache size of an image.[/+]on Mar 18, 2020
In the mean time, you could probably use Image.memory with the code I linked in that document to read the size ahead of time, and do your own work to properly use cacheHeight and cacheWidth as a work around.
Oh, and I should add for completeness - it would really be best if you just shipped the asset at the right scale. You're still going to use more CPU and memory (and network or disk bandwidth) to scale the image on device than if it was just decoded at the right size and scale to begin with.
Oh, and I should add for completeness - it would really be best if you just shipped the asset at the right scale. You're still going to use more CPU and memory (and network or disk bandwidth) to scale the image on device than if it was just decoded at the right size and scale to begin with.
My English is not good. I'm not sure what you mean by messages above.I'm just use local image files, so don't need to use the network, the main impact is memory usage and performance
I'm realizing there are actually two requests here: one is to better be able to maintain the aspect ratio, the other is to decode part of an image.
I'm not sure we can fulfill that second part, because I don't think all the codecs we support even have the option to do partial decoding without decoding the full image.
Ok. I'm going to close this issue at this point. Firstly because it's just not feasible for us to support decoding a subset of an image without decoding the full image, and secondly because we do support maintaining aspect ratio while resizing. While the image API could use some improvements, I don't think this bug is a good place to track those.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Activity
goderbauer commentedon Mar 18, 2020
/cc @dnfield You were thinking about making this better :)
[-]_instantiateImageCodec optimization wanted[/-][+]Maintain aspect ratio when changing the cache size of an image.[/+]dnfield commentedon Mar 18, 2020
Yes - see https://flutter.dev/go/encoded-image-dimensions. The trick is finding some way to make this useful for people with the current
Imagewidget API.dnfield commentedon Mar 18, 2020
I have ideas for this. Assigning a conservative milestone for now.
taosimple commentedon Mar 19, 2020
@dnfield Thank you for your reply
I have tested
package:imagementioned in https://flutter.dev/go/encoded-image-dimensions , It can achieve the purpose by resize and crop, but have 2 problems:Can't work with flutter MemoryCache
About 100 times slower than instantiateImageCodec
1 remaining item
dnfield commentedon Mar 19, 2020
In the mean time, you could probably use
Image.memorywith the code I linked in that document to read the size ahead of time, and do your own work to properly usecacheHeightandcacheWidthas a work around.dnfield commentedon Mar 19, 2020
Oh, and I should add for completeness - it would really be best if you just shipped the asset at the right scale. You're still going to use more CPU and memory (and network or disk bandwidth) to scale the image on device than if it was just decoded at the right size and scale to begin with.
taosimple commentedon Mar 19, 2020
My English is not good. I'm not sure what you mean by messages above.I'm just use local image files, so don't need to use the network, the main impact is memory usage and performance
dnfield commentedon Mar 19, 2020
Can you just resize the images ahead of time, and include images that are the correct size/scale in your application bundle?
taosimple commentedon Mar 19, 2020
In principle, it is feasible, but the processing is more complicated, which needs create a map between thumbnail and src image.
taosimple commentedon Mar 19, 2020
I referenced the principle of the Glide, I think i should resize the images ahead of time and generate thumbnail, Thanks again.
dnfield commentedon Aug 19, 2020
Changes needed in the engine have landed for this. I'm going to look at how we can make ResizeImage better utilize them.
dnfield commentedon Aug 21, 2020
I'm realizing there are actually two requests here: one is to better be able to maintain the aspect ratio, the other is to decode part of an image.
I'm not sure we can fulfill that second part, because I don't think all the codecs we support even have the option to do partial decoding without decoding the full image.
dnfield commentedon Aug 21, 2020
Ok. I'm going to close this issue at this point. Firstly because it's just not feasible for us to support decoding a subset of an image without decoding the full image, and secondly because we do support maintaining aspect ratio while resizing. While the image API could use some improvements, I don't think this bug is a good place to track those.
MatrixDev commentedon Mar 4, 2021
I will leave it here in case someone needs this in the future:
github-actions commentedon Aug 5, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.