Swift 裁剪 UIImage 失败

Swift cropping UIImage fails

我写了下面的函数:

fileprivate func crop(image: UIImage, rect: CGRect) -> CGImage? {
        debugPrint("imageHeight: \(image.cgImage?.height)")
        debugPrint("imageWidth: \(image.cgImage?.width)")
        debugPrint("cropZone: \(rect)")

        guard let cropped = image.cgImage?.cropping(to: rect) else {
            debugPrint("cropping process failed")
            return nil
        }
        
        debugPrint("cropped")
        return cropped
    }

这给了我输出:

"imageHeight: Optional(4032)"
"imageWidth: Optional(3024)"
"cropZone: (3024.5166500171667, 1605.1421763895605, 949.3006961766132, 251.7156472208785)"
[Unknown process name] CGImageCreateWithImageProvider: invalid image size: 0 x 0.
"cropping process failed"

但我不知道为什么裁剪过程会失败。

图片宽度为 3024,但裁剪矩形的 x 坐标为 3024.5...在图片之外。