如何使用Swift裁剪给定角度的图像

3

有人知道如何使用Swift裁剪带有给定角度的图像吗? 我在下面放了演示图片。 我谷歌了一下,发现几乎所有的解决方案都是关于无旋转或90度旋转的图像。 我想旋转图像然后像iPhone上的“照片”应用程序一样裁剪它。 感谢任何提示!

Crop an image with a given angle

2个回答

1
一种选择是使用CGContext和CGAffineTransform根据您的角度进行旋转。
制作两个矩形,一个用于旋转图像,另一个用于裁剪图像,并使用cropping(to rect: CGRect) -> CGImage?进行裁剪。
最后根据您的逻辑制作一个或两个图像,这完全取决于您的方法。
这是一个很好的参考资料:

https://www.raywenderlich.com/2305-core-image-tutorial-getting-started

希望它有所帮助。

0

设计故事板并在ViewController类中创建出口和属性。

let picker = UIImagePickerController()
var circlePath = UIBezierPath()
@IBOutlet weak var crop: CropView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var scroll: UIScrollView!

属性crop具有自定义的UIView类。在其中添加以下委托函数。
func point(inside point: CGPoint, with event:   UIEvent?) -> Bool{
return false
}

创建UIImage和UIImageView的扩展 - 参考。为了缩放图像,请使用委托函数viewForZooming,然后将UIScrollViewDelegate添加到类中作为子类型并返回imageView。
从相册中选择图像 - 创建IBAction,从相册中选择图像并将选择器源类型设置为照片库,请使用以下代码。
picker.sourceType = .photoLibrary
present(picker, animated: true, completion: nil)

并将UIImagePickerControllerDelegate添加为类的子类型。在viewDidLoad中,

picker.delegate = self

使用 didFinishPickingMediaWithInfo —UIImagePickerControllerDelegate 函数从相册选择图片后,将图片设置到视图中。
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = chosenImage.resizeImage()
dismiss(animated:true, completion: nil)

如果您要在取消时关闭照片相册,请使用imagePickerControllerDidCancel委托。

从相机拍摄照片 - 创建IBAction以从相机拍摄图像。首先,检查设备中是否可用SourceTypeAvailable。如果设置了拾取器相机捕获模式为照片。否则处理该操作。然后将源类型设置为相机,相机捕获模式设置为照片。

if UIImagePickerController.isSourceTypeAvailable(.camera){  
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode =  UIImagePickerControllerCameraCaptureMode.photo
picker.modalPresentationStyle = .custom
present(picker,animated: true,completion: nil)
}else{
//action performed if there is no camera in device.
}

裁剪 —

将子图层添加到选定的图像中 - 此图层提供了一个区域来修复裁剪框。

let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height), cornerRadius: 0)

将路径分配给类型为UIBezierPath的圆形路径属性。使用BezierPath,您可以将裁剪框架更改为不同的形状。

circlePath = UIBezierPath(roundedRect: CGRect(x: (self.view.frame.size.width / 2) - (size/2), y: (self.view.frame.size.height / 2) - (size / 2), width: size, height: size, cornerRadius: 0)
path.append(circlePath)

在其坐标空间中绘制立方贝塞尔样条的CAShapeLayer。

let fillLayer = CAShapeLayer()
fillLayer.path = path.cgPath

最后,将该层添加到视图中。
view.layer.addSublayer(fillLayer)

添加裁剪区域: 创建裁剪区域。为此,我们需要设置因子,将imageView图像宽度除以视图框架宽度设置比例。
let factor = imageView.image!.size.width/view.frame.width

用滚轮缩放来进行缩放,zoomScale。

let scale = 1/scroll.zoomScale

然后设置裁剪区域框架(x,y,宽度,高度)。

let x = (scroll.contentOffset.x + circlePath.bounds.origin.x - imageFrame.origin.x) * scale * factor
let y = (scroll.contentOffset.y + circlePath.bounds.origin.y - imageFrame.origin.y) * scale * factor
let width =  circlePath.bounds.width  * scale * factor
let height = circlePath.bounds.height  * scale * factor

最后,创建一个IBAction来裁剪图像。
let croppedCGImage = imageView.image?.cgImage?.cropping(to: croparea)
let croppedImage = UIImage(cgImage: croppedCGImage!)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接