Ios 如何将CAShapeLayer坐标转换为CIRectangleFeature以进行手动裁剪

Ios 如何将CAShapeLayer坐标转换为CIRectangleFeature以进行手动裁剪,ios,crop,core-image,cashapelayer,Ios,Crop,Core Image,Cashapelayer,我正在处理手动图像裁剪功能。对于此am绘图,使用四个坐标(称为topLeft、topRight、bottomRight和bottomLeft)的图层。用户可以平移点并选择裁剪区域。 我一直致力于将这些点转换为核心图像坐标,并使用CIPerspectiveTransform进行进一步裁剪 将图像设置为imageView,并使用形状层屏蔽imageView self.imageView = img imageView.layer.mask = shapeLayer // set here your

我正在处理手动图像裁剪功能。对于此am绘图,使用四个坐标(称为topLeft、topRight、bottomRight和bottomLeft)的图层。用户可以平移点并选择裁剪区域。 我一直致力于将这些点转换为核心图像坐标,并使用CIPerspectiveTransform进行进一步裁剪


将图像设置为imageView,并使用形状层屏蔽imageView

self.imageView = img
imageView.layer.mask = shapeLayer // set here your shape layer
现在,您的imageView具有裁剪图像

从当前上下文中获取图像,如下所示

UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, 0);
imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// check cropping image
let resultImageView = UIImageView(frame: self.view.frame)
resultImageView.image = image
self.view.addSubview(resultImageView)

我想你们试着把图像裁剪成shaplayer?嗨,谢谢你们的回复。我使用用户的可视化形状层。我打算做的是得到用户形成的这四个形状点,并根据这些点裁剪图像。我在将这些UIKit点转换为CoreImage点时遇到问题。请提供形状层初始值Var cropRectOverlay=CAShapeLayer()cropRectOverlay.fillColor=UIColor(红色:CGFloat(25/255.0)、绿色:CGFloat(17/255.0)、蓝色:CGFloat(100/255.0)、alpha:CGFloat(0.3)).cgColor cropRectOverlay.lineWidth=3.0 cropRectOverlay.strokeColor=UIColor(红色:CGFloat(25/255.0)、绿色:CGFloat(17/255.0)、蓝色:CGFloat(100/255.0)、alpha:CGFloat(1.0)).cgColor为左上、右下、左下和右上左上按钮添加四个按钮。中心=CGPoint(x:cropImageView.bounds.origin.x+50,y:cropImageView.bounds.origin.y+50)bottomRightButton.center=CGPoint(x:cropImageView.bounds.size.width-50,y:cropImageView.bounds.size.height-50)topRightButton.center=CGPoint(x:cropImageView.bounds.size.width-50,y:cropImageView.bounds.origin.y+50)bounds=CGPoint.center=CGPoint(x:cropImageView.bounds.origin.x+50,y:cropImageView.bounds.size.height-5)谢谢,效果非常好。以下是截图和合成图像:file:///Users/Swapnil/Desktop/IMG_2082.PNG file:///Users/Swapnil/Desktop/IMG_2083.JPG 我需要您的帮助。@SwapnilDhapte我无法显示您的结果。我能为您提供什么帮助?有什么方法可以只保存渲染部分的图像,而不保存白色背景。As我想对其应用CIPerspectiveTransform。请将图像背景色设置为清晰,然后尝试保存。无法在此处发布图像,因此我已自行回答问题:(