【发布时间】:2021-12-03 01:35:01
【问题描述】:
我创建了一个用户可以拍照的应用,我想添加一个自定义裁剪功能,该功能类似于 Snapchats 或 PhotoShops 魔术棒工具。我从 Apple 开发者网站上找到了下面的代码,它允许我拍摄捕获的图像并将其裁剪为 CGRect。
func cropImage(_ inputImage: UIImage, toRect cropRect: CGRect, viewWidth: CGFloat, viewHeight: CGFloat) -> UIImage?
{
let imageViewScale = max(inputImage.size.width / viewWidth,
inputImage.size.height / viewHeight)
// Scale cropRect to handle images larger than shown-on-screen size
let cropZone = CGRect(x:cropRect.origin.x * imageViewScale,
y:cropRect.origin.y * imageViewScale,
width:cropRect.size.width * imageViewScale,
height:cropRect.size.height * imageViewScale)
// Perform cropping in Core Graphics
guard let cutImageRef: CGImage = inputImage.cgImage?.cropping(to:cropZone)
else {
return nil
}
// Return image to UIImage
let croppedImage: UIImage = UIImage(cgImage: cutImageRef)
return croppedImage
}
除了使用GCRect,我如何在图像中的对象周围绘制并将其用作cropZone?任何帮助将不胜感激。
【问题讨论】:
-
您是否在问如何创建允许用户选择图像区域的 UI?您需要创建一个可以跟踪用户的“橡皮筋”选择手势的视图,在跟踪用户输入的同时绘制正确的反馈,并成功地将用户输入转换为图像坐标空间中的 CGRect。对于一个简单的 Stack Overflow 问题来说,这是一个非常大的话题。