Skip to content

Commit

Permalink
Add resizeMethod type 'never' to disable downsampling on Android
Browse files Browse the repository at this point in the history
Summary:
## Summary
Adds a new `resizeMethod` called `never`. It disables downsampling on the image request and disregards the global image pipeline default. This has been a pain point raised when rendering large images on Android in issues like [this one](facebook#21301) and [this one](facebook/fresco#2397).

## Changelog
[Android][Added] - Adds a new `resizeMethod`, `never`, which disables downsampling for an image

Differential Revision: D62393211
  • Loading branch information
Abbondanzo authored and facebook-github-bot committed Oct 7, 2024
1 parent abde7ae commit 6dfcffb
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Image/ImageProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type AndroidImageProps = $ReadOnly<{|
* dimensions differ from the image view's dimensions. Defaults to `'auto'`.
* See https://reactnative.dev/docs/image#resizemethod-android
*/
resizeMethod?: ?('auto' | 'resize' | 'scale'),
resizeMethod?: ?('auto' | 'resize' | 'scale' | 'never'),

/**
* When the `resizeMethod` is set to `resize`, the destination dimensions are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4825,7 +4825,7 @@ type AndroidImageProps = $ReadOnly<{|
loadingIndicatorSource?: ?(number | $ReadOnly<{| uri: string |}>),
progressiveRenderingEnabled?: ?boolean,
fadeDuration?: ?number,
resizeMethod?: ?(\\"auto\\" | \\"resize\\" | \\"scale\\"),
resizeMethod?: ?(\\"auto\\" | \\"resize\\" | \\"scale\\" | \\"never\\"),
resizeMultiplier?: ?number,
|}>;
export type ImageProps = {|
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -6632,6 +6632,7 @@ public final class com/facebook/react/views/image/ImageLoadEvent$Companion {

public final class com/facebook/react/views/image/ImageResizeMethod : java/lang/Enum {
public static final field AUTO Lcom/facebook/react/views/image/ImageResizeMethod;
public static final field NEVER Lcom/facebook/react/views/image/ImageResizeMethod;
public static final field RESIZE Lcom/facebook/react/views/image/ImageResizeMethod;
public static final field SCALE Lcom/facebook/react/views/image/ImageResizeMethod;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ constructor(
// the 'last' ReactActivity is being destroyed, which effectively means the app is being
// backgrounded.
if (hasBeenInitialized() && clearOnDestroy) {
imagePipeline!!.clearMemoryCaches()
imagePipeline?.clearMemoryCaches()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public enum class ImageResizeMethod {
AUTO,
RESIZE,
SCALE,
NEVER,
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ public constructor(
when (resizeMethod) {
null,
"auto" -> view.setResizeMethod(ImageResizeMethod.AUTO)

"resize" -> view.setResizeMethod(ImageResizeMethod.RESIZE)
"scale" -> view.setResizeMethod(ImageResizeMethod.SCALE)
"never" -> view.setResizeMethod(ImageResizeMethod.NEVER)
else -> {
view.setResizeMethod(ImageResizeMethod.AUTO)
FLog.w(ReactConstants.TAG, "Invalid resize method: '$resizeMethod'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.facebook.drawee.generic.RoundingParams
import com.facebook.drawee.view.GenericDraweeView
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory
import com.facebook.imagepipeline.common.ResizeOptions
import com.facebook.imagepipeline.core.DownsampleMode
import com.facebook.imagepipeline.image.CloseableImage
import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.postprocessors.IterativeBoxBlurPostProcessor
Expand Down Expand Up @@ -415,6 +416,10 @@ public class ReactImageView(
.setAutoRotateEnabled(true)
.setProgressiveRenderingEnabled(progressiveRenderingEnabled)

if (resizeMethod == ImageResizeMethod.NEVER) {
imageRequestBuilder.setDownsampleOverride(DownsampleMode.NEVER)
}

val imageRequest: ImageRequest =
ReactNetworkImageRequest.fromBuilderWithHeaders(imageRequestBuilder, headers)

Expand All @@ -435,14 +440,16 @@ public class ReactImageView(
callerContext?.let { builder.setCallerContext(it) }

cachedImageSource?.let { cachedSource ->
val cachedImageRequest =
val cachedImageRequestBuilder =
ImageRequestBuilder.newBuilderWithSource(cachedSource.uri)
.setPostprocessor(postprocessor)
.setResizeOptions(resizeOptions)
.setAutoRotateEnabled(true)
.setProgressiveRenderingEnabled(progressiveRenderingEnabled)
.build()
builder.setLowResImageRequest(cachedImageRequest)
if (resizeMethod == ImageResizeMethod.NEVER) {
cachedImageRequestBuilder.setDownsampleOverride(DownsampleMode.NEVER)
}
builder.setLowResImageRequest(cachedImageRequestBuilder.build())
}

if (downloadListener != null && controllerForTesting != null) {
Expand Down

0 comments on commit 6dfcffb

Please sign in to comment.