Fix unused inSampleSize and add scaling to decoding step #359
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm looking for ways to optimise memory usage in my app, and found some things in
BitmapPhotoTransformer
that i think can be improved.There is
inSampleSize
calculation being done for decoding options, but it is never used because of typo.We can skip creating another bitmap with
Bitmap.createScaledBitmap
if we add scaling step to decoding.This way, if we want to scale down image while retaining original aspect ratio we skip
Bitmap.createScaledBitmap
, thus avoiding another bitmap allocation.If we need to scale irregardless of aspect ratio, then
createScaledBitmap
will be used, but on already scaled down by biggest side version of the image, which is also a plus.I also tried to measure performance benefit of memory usage.
I've modified sample to use
.toBitmap(scaled(scaleFactor = 0.22f))
Otherwise scaleFactor pf 0.25 results in inSampleSize = 4, which wouldn't need any scaling afterwards.
Not saying this is accurate (not sure how to really measure it here), but you can see that it does have some effect.
So, sample without changes is about 180-200MB:
(uses
createScaledBitmap
)https://i.imgur.com/JjtFVGf.png
Sample that first downsamples by
inSampleSize
and then scales bycreateScaledBitmap
is about 107-120MB:
https://i.imgur.com/T49QVbD.png
Sample that downsamples and scales in one step and skips allocation of
createScaledBitmap
:is about 98-102MB:
https://i.imgur.com/4AI1jMU.png