From 9c7246ec310c9b70d2d6b93ca605abc979e2e448 Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Wed, 2 Dec 2026 14:53:01 +0100 Subject: [PATCH] fix(android): decode the crop region, not the whole image, when resizing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With `displaySize`, `inSampleSize` was computed from the crop rect but the decode asked for `Rect(0, 0, decoder.width, decoder.height)` — the entire source image. So a small crop of a huge photo picked a small sample size and allocated a full-resolution bitmap: a 10000x7500 panorama cropped to 200x200 with `displaySize {100, 100}` decoded to 5000x3750 ARGB_8888, about 75 MB, to produce a 100x100 thumbnail. Passing `displaySize` made memory strictly worse than omitting it, and the intermediate bitmap was never recycled. `cropTask` already decoded just the crop rect. Decode the orientation-mapped crop rect instead and size `inSampleSize` from the region actually decoded. Take the downsample factor from the decoded bitmap rather than assuming `inSampleSize` was honoured, since `decodeRegion` ignores it on API 21-23 (minSdk is 21), and recycle the decoded bitmap once it has been scaled (unless `createBitmap` returned that same instance). Also recycle the decoder, which this path never released. --- .../imageeditor/ImageEditorModuleImpl.kt | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt index 53a92f3..f19696c 100644 --- a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt +++ b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt @@ -325,28 +325,39 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { // Decode the bitmap. We have to open the stream again, like in the example linked // above. // Is there a way to just continue reading from the stream? - outOptions.inSampleSize = getDecodeSampleSize(width, height, targetWidth, targetHeight) + // Decode the crop region, never the whole image: `inSampleSize` is derived from the + // region, so decoding everything else allocates a full-resolution bitmap just to + // throw almost all of it away. + val region = + Rect( + newX.roundToInt(), + newY.roundToInt(), + (newX + newWidth).roundToInt(), + (newY + newHeight).roundToInt() + ) + outOptions.inSampleSize = + getDecodeSampleSize(region.width(), region.height(), targetWidth, targetHeight) + + val bitmap = + try { + decoder.decodeRegion(region, outOptions) + ?: throw IOException("Cannot decode bitmap: $uri") + } finally { + decoder.recycle() + } - val cropX = (newX / outOptions.inSampleSize.toFloat()).roundToInt() - val cropY = (newY / outOptions.inSampleSize.toFloat()).roundToInt() - val cropWidth = (newWidth / outOptions.inSampleSize.toFloat()).roundToInt() - val cropHeight = (newHeight / outOptions.inSampleSize.toFloat()).roundToInt() - val cropScale = scale * outOptions.inSampleSize + // The decoded region is the crop, only downsampled by whatever factor the decoder + // actually applied: `decodeRegion` ignores `inSampleSize` below API 24. + val cropScale = scale * region.width() / bitmap.width.toFloat() val scaleMatrix = Matrix().apply { setScale(cropScale, cropScale) } val filter = true - val rect = Rect(0, 0, decoder.width, decoder.height) - val bitmap = decoder.decodeRegion(rect, outOptions) - - return Bitmap.createBitmap( - bitmap, - cropX, - cropY, - cropWidth, - cropHeight, - scaleMatrix, - filter - ) + val cropped = + Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, scaleMatrix, filter) + if (cropped !== bitmap) { + bitmap.recycle() + } + return cropped } }