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)
}
}