Commit 43a7280
committed
fix(ios): correct crop rect size, orientation, and renderer scale
Four related bugs in iOS caused Image.crop() / Image.resize() (and,
by extension, Image.rotate() slow path and ImageFactory.createBlankImage)
to produce incorrect output. They surface most visibly through the
vision-camera v5 flow Photo.toImageAsync() -> crop -> resize ->
saveToTemporaryFileAsync(), but repro from any source image.
ios/Public/NativeImage.swift:
1. crop: the targetRect used CGSize(uiImage.size.width, uiImage.size.height)
instead of the computed (targetWidth, targetHeight). CGImage.cropping
intersects the rect with the image bounds, so callers got "image minus
origin offset" instead of an actual crop of the requested region.
2. crop: the pre-crop UIImage could have non-.up imageOrientation and/or
scale != 1 (both common for the Photo.toImageAsync() bridge, which
constructs UIImage(cgImage:, scale: 1, orientation: uiOrientation)).
uiImage.size is display-space while cgImage is sensor-space, so the
caller's rect landed in the wrong region of cgImage. UIImage(cgImage:)
also defaulted the output to .up orientation, dropping the source's
orientation and producing sensor-oriented pixels.
Fix: if orientation != .up or scale != 1, draw through a scale=1
UIGraphicsImageRenderer to bake both into pixels before cropping,
then wrap the cropped CGImage with explicit scale=1 and .up so
downstream consumers see consistent state.
3. resize + rotate (slow path): UIGraphicsImageRenderer(size:) without
a UIGraphicsImageRendererFormat defaults to UIScreen.main.scale.
On 2x/3x devices "resize to 2000x1500" produced 4000x3000 or
6000x4500-pixel output, while uiImage.size still reported
(2000, 1500) in points. JS callers saw correct Image.width/height
but the saved JPEG was 2-3x too large in each dimension.
Fix: set UIGraphicsImageRendererFormat().scale = 1 before constructing
the renderer.
ios/HybridImageFactory.swift:
4. createBlankImage had the same default-scale issue; applied the same
scale = 1 fix.
Android (HybridImage.kt) is unchanged -- its Bitmap-based path has no
point/pixel distinction and was already correct.
Verified on an iPhone 15 Pro (3x screen scale) via a vision-camera v5
photo-scanning app. Before: cropped region was wrong, orientation
dropped to landscape sensor-space, saved files 3x larger than requested.
After: crop produces the requested region in portrait orientation, and
saved files match the requested pixel dimensions.1 parent 80e8088 commit 43a7280
2 files changed
Lines changed: 51 additions & 8 deletions
Lines changed: 5 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| 24 | + | |
21 | 25 | | |
22 | 26 | | |
23 | 27 | | |
| |||
Lines changed: 46 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
66 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
67 | 72 | | |
68 | 73 | | |
69 | 74 | | |
| |||
97 | 102 | | |
98 | 103 | | |
99 | 104 | | |
100 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
101 | 114 | | |
102 | 115 | | |
103 | 116 | | |
| |||
120 | 133 | | |
121 | 134 | | |
122 | 135 | | |
123 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
124 | 159 | | |
125 | 160 | | |
126 | 161 | | |
127 | | - | |
128 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
129 | 166 | | |
130 | 167 | | |
131 | 168 | | |
132 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
133 | 172 | | |
134 | 173 | | |
135 | 174 | | |
| |||
0 commit comments