Repository navigation
fix(ios): reject invalid quality before downloading; document why the full-res decode is deliberate - #222
Merged
Conversation
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.
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two iOS findings. One is fixed; the other is not, and the review that produced it was wrong about the fix. Read the H10 section before reviewing anything else.
H12 (
b3e3140) — fixed: invalidqualitydiscovered only after download + decodecropImage(remoteUrl, { quality: 5 })downloaded and fully decoded the source image before rejecting. The range check now runs immediately afteradaptParamsWithFormat:, before theNSMutableURLRequestis built and beforeloadImageWithURLRequest:. The old in-callback check is removed, so this is a pure move, not a duplicate.Error code (
RCTErrorUnspecified) and message string ("quality must be a number between 0 and 1") are byte-identical, so apps matching on them are unaffected.paramsis captured by value in both positions — no lifetime change.Safety of an early
returnverified against RN source:RCTTurboModule.mm:404(invokeCopy(resolveBlock, rejectBlock)) invokes the method synchronously inside the Promise constructor executor, so an earlyrejectsettles the promise exactly once with both blocks live. Every path settles exactly once: early quality reject, loadererrorreject,writeErrorreject, finalresolve.Deliberately NOT changed:
formatformat: 'webp'still silently produces JPEG rather than erroring. There is no late error to hoist — persrc/NativeRNCImageEditor.ts:50, a format other thanpng/jpegmeans "default auto-detection based on given image", andwebpis a documented public value (src/types.ts:10,CropResult.typeincludes'image/webp'). So it isn't an invalid value; turning it into a rejection is a new behavior change that breaks existing iOS callers while Android still writes webp. That fallback — and its real bug, that iOS detects format from the URL path extension only, so extension-less/data:/ph://URLs always land on JPEG — is a separate finding, left for its own PR.offset/sizelikewise got no hoisted check: both are required by codegen, so there's no optional to test for presence (which is all Android's up-front check does), and a bounds check structurally cannot run before the download since it needs decoded dimensions.H10 — NOT fixed. The memory bug is real; the proposed fix is wrong
The diagnosis stands and is confirmed: the 2-arg
loadImageWithURLRequest:passessize:CGSizeZero, scale:1(now verified verbatim atRCTImageLoader.mm:361-374), which reachesRCTDecodeImageWithDatawhere a zerodestSizebecomessourceSize→createThumbnailfalse → full-resolutionCGImageSourceCreateImageAtIndex. A 100×100 crop of a 12 MP photo decodes the whole image.But downsampling before the crop is not safe here, and I could not make it correct. I picked the "reachable" route the review suggested, verified it end-to-end, and hit four blockers:
sizeis catastrophic, not merely lossy.offset/sizeare in source pixels. A 100×100 crop of a 4000×3000 source givesRCTTargetSize(cover)→ 133×100, sokCGImageSourceThumbnailMaxPixelSize= 133 — the source shrinks ~30× before the crop. The crop region collapses from 100×100 source px to ~3×3 px, whichRCTTransformImagethen upsamples back to 100×100. A pixel-exact crop becomes blurred mush. The root problem: inRCTTransformImage(image, targetSize, …)the source region and the output size are the same value, so you cannot downsample tosizeand still crop atsize.displaySize(what Android does) needs the source pixel dimensions, which iOS doesn't have. The decode returns[UIImage imageWithCGImage:imageRef scale:destScale …], so.sizeis downsampled points at an arbitrary scale and the source dims are unrecoverable. Recovering them means either a second metadata fetch (getImageSizeForURLRequest:— near-free for localfile://, a full re-download for remote) or bypassingRCTImageLoaderand losing its caching,ph://support and header handling. Getting the ratio wrong is a silent wrong crop.UIImageOrientationUp(RCTImageUtils.mm:326-336, confirmed:kCGImageSourceCreateThumbnailWithTransform: @YESwith a comment saying exactly that); the full-res path returns an orientation-taggedUIImage. The crop math readsimage.size, so switching branches silently changes crop geometry for the common camera-JPEG case.clipped:YESwould additionally applyRCTResizeImageIfNeededto the decoded image before the crop — another resample to neutralize viaclipped:NO.The minimal correct subset (downsample to
displaySize, source dims via a metadata pass) is a rewrite of the load path with a network round-trip on remote URLs — not a minimal diff, and untestable in this environment. Reachability of a selector is not reachability of a correct fix.So this PR ships the corrected documentation only:
:79-82— the doc comment claimeddisplaySize"is an optimization - if specified, the image will be scaled down todisplaySizerather thansize". Both halves are false: it isn't an optimization, and it scales todisplaySizein addition tosize, not rather than it.:132-139— a comment at the load site recording that the full-resolution decode is deliberate and why, so the next reader doesn't spend an afternoon rediscovering this.displaySizedoes not reduce peak decode memory today. That remains an open bug. Also worth knowing: Android's non-resize path usesBitmapRegionDecoder, a genuine region decode with no full-image decode at all. ImageIO has no equivalent, so on that path iOS can only approach Android, never match it. Any real H10 fix should be its own PR with a plan to get source dimensions without a re-download, plus device measurements.Verification status
Not compiled — no Pods install, no simulator. Unverified: compilation, crop output, and all memory figures (the ~49 MB number is the review's estimate, not a measurement; the H10 improvement here is zero, since behavior is unchanged).
Verified by reading the pinned RN 0.87.1 in
node_modules: theRCTImageLoaderselectors exist;CGSizeZero/scale:1defaults; the thumbnail transform-bake; theRCTIsMainQueue()offload atRCTImageLoader.mm:969-981;RCTTurboModule.mm:404synchronous invocation. No new ARC captures or retain cycles (H12 adds no block; H10 is comments only). NoCGFloat/points-vs-pixels logic touched. Each commit is self-consistent standalone — the quality check is present in both, so reverting H12 alone leaves it in the callback.Left for other PRs (from the same review)
displaySizewidth-xor-height skip at:153(&&againstDEFAULT_DISPLAY_SIZE0)