From 98b3d5b0717f6dd58554fefb931ce4fd9770ac0c Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Wed, 2 Dec 2026 15:11:47 +0100 Subject: [PATCH 1/2] docs(ios): correct misleading displaySize doc comment The doc comment claimed displaySize was an optimization that scaled the image down "rather than size". It is neither: displaySize is applied by a second RCTTransformImage pass after the full-resolution decode, so the source is always decoded at full resolution regardless. Add a note at the loadImageWithURLRequest: call site recording why the decode is deliberately full-resolution, so the next reader doesn't assume the sized loader overload is a one-line fix. offset/size are in source pixels and ImageIO can only downsample the whole image, so downsampling before the crop shrinks the crop region itself. Android only downsamples this way in cropAndResizeTask, where it rescales offset/size into downsampled space using the source pixel dimensions - which the loaded UIImage does not expose. Behaviour is unchanged; this is documentation only. The memory issue is real and left unfixed. --- ios/RNCImageEditor.mm | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ios/RNCImageEditor.mm b/ios/RNCImageEditor.mm index 60ce3ac..313127e 100644 --- a/ios/RNCImageEditor.mm +++ b/ios/RNCImageEditor.mm @@ -76,8 +76,10 @@ - (Params)adaptParamsWithFormat:(id)format * @param imageRequest An image URL * @param cropData Dictionary with `offset`, `size` and `displaySize`. * `offset` and `size` are relative to the full-resolution image size. - * `displaySize` is an optimization - if specified, the image will - * be scaled down to `displaySize` rather than `size`. + * `displaySize` is the size the cropped image is scaled to. It is not a + * decode-time optimization: the source is always decoded at full + * resolution first (see the note above `loadImageWithURLRequest:`), and + * `displaySize` is applied afterwards, to the already-decoded crop. * All units are in px (not points). */ - (void) cropImage:(NSString *)uri @@ -110,6 +112,14 @@ - (void) cropImage:(NSString *)uri extension = params.format; } + // Decoded at full resolution on purpose (the two-argument loader resolves this to + // `size: CGSizeZero`). `offset`/`size` are in source pixels, and ImageIO has no + // equivalent of Android's BitmapRegionDecoder: it can only downsample the whole + // image, which would shrink the crop region with it. Taking a 100x100 crop from a + // 4000x3000 source downsampled to fit 100x100 would resample the crop from a region + // only a few px across. Downsampling to `displaySize` instead - what Android does in + // cropAndResizeTask - needs the source pixel size to rescale `offset`/`size` into + // downsampled space first, so it is not just a matter of passing a size here. [[_bridge moduleForName:@"ImageLoader" lazilyLoadIfNecessary:YES] loadImageWithURLRequest:imageRequest callback:^(NSError *error, UIImage *image) { if (error) { reject(@(error.code).stringValue, error.description, error); From b3e3140903b9248e1908dc12610d5425001489a7 Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Wed, 2 Dec 2026 15:14:39 +0100 Subject: [PATCH 2/2] fix(ios): validate quality before downloading the source image `quality` was checked inside the image loader callback, so cropImage(remoteUrl, {quality: 5}) transferred and decoded the whole source image before rejecting. The value is only used by UIImageJPEGRepresentation at the end, so nothing about it depends on the decoded image: move the check above loadImageWithURLRequest: so it fails immediately, as Android already does. The error code and message are unchanged, so callers that match on them keep working. `format`, `offset` and `size` are left alone deliberately: - `format` has no late error to hoist. Anything other than png/jpeg is documented as "auto-detect from the URL" and webp is in the public type, so it is not an invalid value - it silently falls back, and that is existing behaviour callers may rely on. Turning it into a rejection would break iOS callers while Android happily writes webp. - `offset`/`size` are required by codegen, and checking them against the decoded dimensions can only happen after the image is loaded. --- ios/RNCImageEditor.mm | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/ios/RNCImageEditor.mm b/ios/RNCImageEditor.mm index 313127e..6f32c64 100644 --- a/ios/RNCImageEditor.mm +++ b/ios/RNCImageEditor.mm @@ -98,6 +98,23 @@ - (void) cropImage:(NSString *)uri quality:@(data.quality().has_value() ? *data.quality() : DEFAULT_COMPRESSION_QUALITY) includeBase64:@(data.includeBase64().has_value() ? *data.includeBase64() : NO) headers: data.headers()]; + + // Validate before downloading and decoding anything: `quality` is only used when the + // result is encoded, so a bad value should fail in microseconds rather than after the + // whole source image has been transferred and decoded. + // + // `format` is deliberately not validated. A format other than `png`/`jpeg` is not an + // error here - it means "auto-detect from the URL", which is the documented default, + // and `webp` is in the public type - so there is no late error to hoist. Rejecting it + // would break existing callers on iOS while Android still writes webp. + // + // `offset`/`size` need no check either: codegen requires them, and validating them + // against the decoded dimensions cannot happen before the image is loaded. + if (params.quality > 1 || params.quality < 0) { + reject(RCTErrorUnspecified, @("quality must be a number between 0 and 1"), nil); + return; + } + NSMutableURLRequest *imageRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: uri]]; [params.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) { if (value) { @@ -125,10 +142,6 @@ - (void) cropImage:(NSString *)uri reject(@(error.code).stringValue, error.description, error); return; } - if (params.quality > 1 || params.quality < 0) { - reject(RCTErrorUnspecified, @("quality must be a number between 0 and 1"), nil); - return; - } // Crop image CGSize targetSize = params.size;