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

Use BitmapRegionDecoder to efficiently crop images on Android #15439

Conversation

faifai21
Copy link
Contributor

@faifai21 faifai21 commented Aug 9, 2017

The Android ImageEditingManager is inefficient and slow when cropping images. It loads the full resolution image into memory and then crops it. This leads to slow performance and occasional OutOfMemory Exceptions.
BitmapRegionDecoder can be used to crop without needing to load the full resolution image into memory. Using it is much more efficient and much faster.

Relevant issue: #10470

Test Plan

Attempt to crop a very large image (2000x2000) on Android. With this change, the crop should happen almost instantly. On the master branch, it should take 2-3 seconds (and might run out of memory).

Please let me know if there's anything else I can provide.

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Aug 9, 2017
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@facebook-github-bot facebook-github-bot added the Import Started This pull request has been imported. This does not imply the PR has been approved. label Aug 15, 2017
@facebook-github-bot
Copy link
Contributor

@shergin has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@shergin has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@rahamin1
Copy link

@faifai21 This is probably a stupid question, but fear is stronger than shame :)
Does this mean that I can use BitmapRegionDecoder in React Native? Is there an example for how to use it?

@noth8
Copy link

noth8 commented Aug 15, 2018

@rahamin1, look ta this code link . So this work's automatically for you when you use in your code. But now I am testing code with many images from api with size >2500px and problems(black empty block instead image, app crashing) occurs same like before that commit with BitmapRegionDecoder. I think image resizing is the only solution if you want to get rid of memory leak with fetching large images. May be @faifai21 has another solution.

@rahamin1
Copy link

rahamin1 commented Aug 16, 2018

Thanks @noth8 I cannot resize since I am displaying previews of web articles in a ListView, and the images are taken from the articles.
I am willing, however, not to display images that are too large. However, if I will use Image.getSize, I am not sure that it will help, since getSize is supposed to load the image in order to check its size.

Hopefully @faifai21 has other insights.

Trancever pushed a commit to Trancever/react-native-image-editor that referenced this pull request Feb 12, 2019
Summary:
The Android ImageEditingManager is inefficient and slow when cropping images. It loads the full resolution image into memory and then crops it. This leads to slow performance and occasional OutOfMemory Exceptions.
[BitmapRegionDecoder](https://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html) can be used to crop without needing to load the full resolution image into memory. Using it is much more efficient and much faster.

Relevant issue: facebook/react-native#10470

Attempt to crop a very large image (2000x2000) on Android. With this change, the crop should happen almost instantly. On the master branch, it should take 2-3 seconds (and might run out of memory).

Please let me know if there's anything else I can provide.
Closes facebook/react-native#15439

Differential Revision: D5628223

Pulled By: shergin

fbshipit-source-id: bf314e76134cd015380968ec4533225e724c4b26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Import Started This pull request has been imported. This does not imply the PR has been approved.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants