feat(Quick): 优化RegionCameraView裁剪算法

重构RegionImageCropper裁剪逻辑,改用纯几何aspect-fill反算方法,
将屏上小框坐标直接映射到照片像素rect,避免使用
metadataOutputRectConverted导致的坐标轴对调问题。

主要变更:
- 移除基于归一化rect的裁剪方式
- 新增cropRect函数进行几何反算
- 修复传感器横向坐标与竖屏照片方向不一致的问题
- 保持裁剪精度的同时提升算法稳定性
```
This commit is contained in:
link2026
2026-05-31 23:51:53 +08:00
parent d72a1fec17
commit 32e7c25ed7
2 changed files with 100 additions and 16 deletions

View File

@@ -7,8 +7,9 @@ import Combine
/// + + **** UIImage
/// (,QuickRegionCaptureFlow 退 PhotoPicker)
///
/// :`previewLayer.metadataOutputRectConverted(fromLayerRect:)`
/// (0-1) rect; bake `.up`, rect CGImage
/// : bake `.up`(), aspect-fill
/// (view ) rect( `RegionImageCropper`)
/// `metadataOutputRectConverted` ,
struct RegionCameraView: View {
let onCapture: (UIImage) -> Void
let onCancel: () -> Void
@@ -198,16 +199,37 @@ enum RegionFraming {
// MARK: -
enum RegionImageCropper {
/// rect( 0-1) `.up`
/// image bake `.up`( `UIImage.normalizedUp()`);退
static func crop(_ image: UIImage, normalizedRect: CGRect) -> UIImage {
/// (view ) `.resizeAspectFill` `.up` rect
/// : aspect-fill viewSize,
/// `metadataOutputRectConverted`(****,
/// x/y ,, RegionImageCropperTests)
static func cropRect(photoPixelSize p: CGSize, box: CGRect, in viewSize: CGSize) -> CGRect {
guard p.width > 0, p.height > 0, viewSize.width > 0, viewSize.height > 0 else { return .zero }
// aspect-fill:,
let scale = max(viewSize.width / p.width, viewSize.height / p.height)
let scaledW = p.width * scale
let scaledH = p.height * scale
// ,
let ox = (viewSize.width - scaledW) / 2
let oy = (viewSize.height - scaledH) / 2
// :,
var x = (box.minX - ox) / scale
var y = (box.minY - oy) / scale
var w = box.width / scale
var h = box.height / scale
//
x = max(0, min(p.width, x))
y = max(0, min(p.height, y))
w = max(0, min(p.width - x, w))
h = max(0, min(p.height - y, h))
return CGRect(x: x, y: y, width: w, height: h).integral
}
/// `.up` (`box` / `viewSize` view );退
static func crop(_ image: UIImage, box: CGRect, viewSize: CGSize) -> UIImage {
guard let cg = image.cgImage else { return image }
let w = CGFloat(cg.width), h = CGFloat(cg.height)
let nx = max(0, min(1, normalizedRect.origin.x))
let ny = max(0, min(1, normalizedRect.origin.y))
let nw = max(0, min(1 - nx, normalizedRect.size.width))
let nh = max(0, min(1 - ny, normalizedRect.size.height))
let rect = CGRect(x: nx * w, y: ny * h, width: nw * w, height: nh * h).integral
let rect = cropRect(photoPixelSize: CGSize(width: cg.width, height: cg.height),
box: box, in: viewSize)
guard rect.width >= 1, rect.height >= 1, let cropped = cg.cropping(to: rect) else { return image }
return UIImage(cgImage: cropped, scale: image.scale, orientation: .up)
}
@@ -334,15 +356,16 @@ final class RegionPreviewUIView: UIView, AVCapturePhotoCaptureDelegate {
return
}
let upright = image.normalizedUp()
guard let preview = previewLayer else {
guard previewLayer != nil else {
deliver(upright)
return
}
// metadataOutputRectConverted previewLayer ,线
// : .resizeAspectFill bounds,,
// aspect-fill rect bounds 线
DispatchQueue.main.async {
let box = RegionFraming.box(in: self.bounds.size)
let normalized = preview.metadataOutputRectConverted(fromLayerRect: box)
let cropped = RegionImageCropper.crop(upright, normalizedRect: normalized)
let viewSize = self.bounds.size
let box = RegionFraming.box(in: viewSize)
let cropped = RegionImageCropper.crop(upright, box: box, viewSize: viewSize)
completion?(cropped)
}
}

View File

@@ -0,0 +1,61 @@
import XCTest
import CoreGraphics
@testable import
///
/// :, > rect
/// `metadataOutputRectConverted`() x/y ,
/// (2026-05-31 bug)
final class RegionImageCropperTests: XCTestCase {
/// + + rect ( > )
func testWideBoxYieldsLandscapeCropRect() {
let photo = CGSize(width: 3024, height: 4032) // ()
let view = CGSize(width: 393, height: 852) // ()
let box = RegionFraming.box(in: view) // 84% / 160
let rect = RegionImageCropper.cropRect(photoPixelSize: photo, box: box, in: view)
XCTAssertGreaterThan(rect.width, rect.height,
"宽框应裁出宽 rect;若 width<height 说明轴对调 bug 复现")
XCTAssertGreaterThan(rect.width, 0)
XCTAssertGreaterThan(rect.height, 0)
}
/// rect ()
func testCropRectStaysInsidePhotoBounds() {
let photo = CGSize(width: 3024, height: 4032)
let view = CGSize(width: 393, height: 852)
let box = RegionFraming.box(in: view)
let rect = RegionImageCropper.cropRect(photoPixelSize: photo, box: box, in: view)
XCTAssertGreaterThanOrEqual(rect.minX, 0)
XCTAssertGreaterThanOrEqual(rect.minY, 0)
XCTAssertLessThanOrEqual(rect.maxX, photo.width)
XCTAssertLessThanOrEqual(rect.maxY, photo.height)
}
/// rect (aspect-fill ,)
func testCropRectPreservesBoxAspectRatio() {
let photo = CGSize(width: 3024, height: 4032)
let view = CGSize(width: 393, height: 852)
let box = RegionFraming.box(in: view)
let rect = RegionImageCropper.cropRect(photoPixelSize: photo, box: box, in: view)
let boxAspect = box.width / box.height
let rectAspect = rect.width / rect.height
XCTAssertEqual(rectAspect, boxAspect, accuracy: 0.05,
"等比映射下,裁剪 rect 宽高比应与屏上小框一致")
}
/// 退:, .zero
func testZeroInputsReturnZero() {
XCTAssertEqual(
RegionImageCropper.cropRect(photoPixelSize: .zero,
box: CGRect(x: 0, y: 0, width: 10, height: 10),
in: CGSize(width: 100, height: 200)),
.zero)
}
}