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

Add Option To Resize Images On Android #401

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

adammcarth
Copy link

As discussed in issue #285, there are some cases where very large, down-sampled images can create performance issues on Android due to the entire image being loaded in the device's RAM.

There is an option in React Native's default <Image /> component which allows you to specify resizeMethod="resize". This uses Facebook's Fresco library to automatically downsample the image itself before being loaded into the devices memory, but since react-native-fast-image uses Glide - there appears to be nothing similar available.

The solution I came up with was to offer the ability to manually down sample the image size before it's loaded into the memory using a new prop called resizeImageAndroid. It uses Glide's .override() method to achieve this.

The only caveat is that you must know the exact image size, but it's nice to at least have this option if it's critical for the performance of large images on Android.

Example Usage:

<View style={{ width: 450, height: 275 }}>
  <FastImage
    source={{ uri: 'http://example.com/huge-image-3600x2200.png' }}
    resizeMode="contain"
    resizeImageAndroid={{ width: 450, height: 275 }} // <-------
  />
</View>

@@ -114,6 +117,13 @@ public void setResizeMode(FastImageViewWithUrl view, String resizeMode) {
view.setScaleType(scaleType);
}

@ReactProp(name = "resizeImageAndroid")
public void setImageResize(FastImageViewWithUrl view, ReadableMap imageSizeOverride) {
// Re-run Glide with width and height override values set.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to put imageResize into source, so we don't have to rerun Glide? It would be beneficial for iOS as well.

@madandrija
Copy link

Any way I can help with getting this PR merged in?

We are currently maintaining a fork with this solution implemented in the latest FastImage release and I'd prefer if I could just use the original and drop the fork 😁

@DylanVann
Copy link
Owner

I believe Glide does downsampling for us without any extra config.

Glide automatically limits the size of the image it holds in cache and memory to the ImageView dimensions.

From https://futurestud.io/tutorials/glide-image-resizing-scaling

@christopherklint97
Copy link

I am also doing as @madandrija states by maintaining a forked version with this pr implemented.

I am not a expert with Glide but I did notice a huge difference when I implemented this solution (pretty much night and day). Really think it would be helpful for other android users to have at their disposal.

@DylanVann
Copy link
Owner

You may be getting better performance because the quality of your rendered images is less if you are not accounting for DPI.

Assuming Glide's auto downsampling works that's the reason I would guess you're seeing better performance.

We could try to create a reproduction of this issue to check if that's the case, or if the auto sampling based on the view size is not working for some reason.

@madandrija
Copy link

I am also doing as @madandrija states by maintaining a forked version with this pr implemented.

I am not a expert with Glide but I did notice a huge difference when I implemented this solution (pretty much night and day). Really think it would be helpful for other android users to have at their disposal.

We've updated our fork to the latest version and still we need the fork in order to speed up image loading (e.g. when scrolling through a FlatList, w/o the fork there were sometimes "blanks" until images loaded, whereas with the fork, everything was running smoothly), and I have two examples of images used.

Regarding repro, still thinking about how to set up a minimal example, either on GitHub or in Snack.

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

Successfully merging this pull request may close these issues.

5 participants