我正在尝试开发一个裁剪图像的应用程序。 通过在图像上触摸两次(topLeft point,bottomRight point)来创建要裁剪的矩形。当用户点击时,屏幕上会显示一个红色矩形以查看裁剪区域。
然后用这个矩形,我想裁剪图像。但我面临一个问题,作物不起作用,给我不好的结果。
在我的代码下面:
@objc
func tap(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: sourceImageView)
print(#function + ":: location: \(location)")
if location1 == nil {
location1 = location
} else if location2 == nil {
location2 = location
print("Processing to crop the image...")
guard let _topLeft = location1,
let _bottomRight = location2 else {
fatalError("Processing failed...")
}
let _topRight = CGPoint(x: _bottomRight.x, y: _topLeft.y)
let _bottomLeft = CGPoint(x: _topLeft.x, y: _bottomRight.y)
let _width = _topLeft.distance(point: _topRight)
let _height = _topLeft.distance(point: _bottomLeft)
let cropArea = CGRect(origin: _topLeft, size: CGSize(width: _width, height: _height))
// Displaying red rectangle on the source image
let outline = CALayer()
outline.frame = cropArea
outline.borderWidth = 2.0
outline.borderColor = UIColor.red.cgColor
sourceImageView.layer.addSublayer(outline)
guard let sourceImage = sourceImageView.image,
let sourceCGImage = sourceImage.cgImage else {
fatalError("Cannot get CG Image from source img")
}
let viewSize = self.view.frame.size
let imageViewScaleWidth = viewSize.width / CGFloat(sourceCGImage.width)
let imageViewScaleHeight = viewSize.height / CGFloat(sourceCGImage.height)
let scale = 1 / min(imageViewScaleWidth, imageViewScaleHeight)
let scaleWidth = 1 / imageViewScaleWidth
let scaleHeight = 1 / imageViewScaleHeight
let newWidth = cropArea.height * scale
let newHeight = cropArea.width * scale
let newXCord = (cropArea.topLeft.x * scale) + newHeight
let newYCord = (cropArea.topLeft.y * scale)
let newCropArea = CGRect(origin: CGPoint(x: newXCord,
y: newYCord),
size: CGSize(width: newHeight,
height: newWidth))
guard let croppedSourceImage = sourceCGImage.cropping(to: newCropArea) else {
print("Cannot crop the current image.")
return
}
}
}
我认为我的问题来自源图像上的矩形投影。
由于
编辑:
public extension CGPoint {
/// Distance between two points.
///
/// - Parameter point: A `CGPoint` instance.
public func distance(point p: CGPoint) -> CGFloat {
return sqrt(pow((p.x - x), 2) + pow((p.y - y), 2))
}
}