diff --git a/ios/RNCImageEditor.mm b/ios/RNCImageEditor.mm index 60ce3ac..6f32c64 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 @@ -96,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) { @@ -110,15 +129,19 @@ - (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); 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;