Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions ios/RNCImageEditor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@
* Crops an image and saves the result to temporary file. Consider using
* CameraRoll API or other third-party module to save it in gallery.
*
* @param imageRequest An image URL

Check warning on line 76 in ios/RNCImageEditor.mm

View workflow job for this annotation

GitHub Actions / Build iOS pods (RN 0.87.1)

parameter 'imageRequest' not found in the function declaration [-Wdocumentation]

Check warning on line 76 in ios/RNCImageEditor.mm

View workflow job for this annotation

GitHub Actions / Build iOS pods (RN latest)

parameter 'imageRequest' not found in the function declaration [-Wdocumentation]

Check warning on line 76 in ios/RNCImageEditor.mm

View workflow job for this annotation

GitHub Actions / Build iOS pods (RN 0.86.3)

parameter 'imageRequest' not found in the function declaration [-Wdocumentation]
* @param cropData Dictionary with `offset`, `size` and `displaySize`.

Check warning on line 77 in ios/RNCImageEditor.mm

View workflow job for this annotation

GitHub Actions / Build iOS pods (RN 0.87.1)

parameter 'cropData' not found in the function declaration [-Wdocumentation]

Check warning on line 77 in ios/RNCImageEditor.mm

View workflow job for this annotation

GitHub Actions / Build iOS pods (RN latest)

parameter 'cropData' not found in the function declaration [-Wdocumentation]

Check warning on line 77 in ios/RNCImageEditor.mm

View workflow job for this annotation

GitHub Actions / Build iOS pods (RN 0.86.3)

parameter 'cropData' not found in the function declaration [-Wdocumentation]
* `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
Expand All @@ -96,6 +98,23 @@
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) {
Expand All @@ -110,15 +129,19 @@
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;
Expand Down
Loading