如何将CAShapeLayer坐标转换为CIRectangleFeature进行手动裁剪 [英] How to convert CAShapeLayer coordinates to CIRectangleFeature for manual crop

查看:170
本文介绍了如何将CAShapeLayer坐标转换为CIRectangleFeature进行手动裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理手动图像裁剪功能。为此,绘制CAShapeLayer的四个坐标分别为topLeft,topRight,bottomRight和bottomLeft。用户可以平移点并选择裁剪区域。
我一直坚持将这些点转换为核心图像坐标,并使用CIPerspectiveTransform进一步裁剪。

I am dealing with manual image crop functionality. For this am drawing CAShapeLayer with four coordinates called topLeft, topRight, bottomRight and bottomLeft. User can pan the points and select the crop area. I am stuck at converting these points to Core image coordinates and further cropping with CIPerspectiveTransform.

推荐答案

将图像设置为imageView并使用形状图层遮盖imageView。

Set image to imageView And masking imageView with shape layer.

self.imageView = img
imageView.layer.mask = shapeLayer // set here your shape layer

现在您的imageView中有裁剪图像。

Now your imageView have crop image.

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

Get image from current context as follow

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)

这篇关于如何将CAShapeLayer坐标转换为CIRectangleFeature进行手动裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆