Files
kangkang/康康Tests/RegionImageCropperTests.swift
link2026 b79ae54b7b ```
feat(iOS): 更新MNN后端模型配置优化性能

将MNN主模型从Qwen3.5-4B(~2.64GiB)降级为Qwen3.5-2B(~1.1GiB),因为4B版本
实测运行过慢,影响用户体验。iPhone17+/SME2设备使用2B模型,保留MLX
兜底方案用于模拟器和备用场景,确保AI推理性能和存储效率的平衡。
```
2026-06-09 22:20:07 +08:00

117 lines
5.3 KiB
Swift

import XCTest
import CoreGraphics
import AVFoundation
@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)
}
// MARK: - aspect-FIT()
/// aspect-fit ; rect
func testAspectFitWideBoxYieldsLandscapeInsideBounds() {
let photo = CGSize(width: 3024, height: 4032)
let view = CGSize(width: 393, height: 852)
let fitted = AVMakeRect(aspectRatio: photo, insideRect: CGRect(origin: .zero, size: view))
// fitted
let box = CGRect(x: fitted.minX + 30, y: fitted.midY - 50, width: fitted.width - 60, height: 100)
let rect = RegionImageCropper.cropRectAspectFit(photoPixelSize: photo, box: box, imageFrame: fitted)
XCTAssertGreaterThan(rect.width, rect.height)
XCTAssertGreaterThanOrEqual(rect.minX, 0)
XCTAssertGreaterThanOrEqual(rect.minY, 0)
XCTAssertLessThanOrEqual(rect.maxX, photo.width)
XCTAssertLessThanOrEqual(rect.maxY, photo.height)
}
/// ()
func testAspectFitFullFrameBoxYieldsFullPhoto() {
let photo = CGSize(width: 2000, height: 1000) //
let view = CGSize(width: 400, height: 800)
let fitted = AVMakeRect(aspectRatio: photo, insideRect: CGRect(origin: .zero, size: view))
let rect = RegionImageCropper.cropRectAspectFit(photoPixelSize: photo, box: fitted, imageFrame: fitted)
XCTAssertEqual(rect.minX, 0, accuracy: 1)
XCTAssertEqual(rect.minY, 0, accuracy: 1)
XCTAssertEqual(rect.width, photo.width, accuracy: 2)
XCTAssertEqual(rect.height, photo.height, accuracy: 2)
}
/// : rect
func testAspectFitPreservesBoxAspectRatio() {
let photo = CGSize(width: 3024, height: 4032)
let view = CGSize(width: 393, height: 852)
let fitted = AVMakeRect(aspectRatio: photo, insideRect: CGRect(origin: .zero, size: view))
let box = CGRect(x: fitted.minX + 20, y: fitted.minY + 40, width: 180, height: 120)
let rect = RegionImageCropper.cropRectAspectFit(photoPixelSize: photo, box: box, imageFrame: fitted)
XCTAssertEqual(rect.width / rect.height, box.width / box.height, accuracy: 0.05)
}
func testAspectFitZeroInputsReturnZero() {
XCTAssertEqual(
RegionImageCropper.cropRectAspectFit(photoPixelSize: .zero,
box: CGRect(x: 0, y: 0, width: 10, height: 10),
imageFrame: CGRect(x: 0, y: 0, width: 100, height: 100)),
.zero)
}
}