【发布时间】:2019-03-30 18:53:37
【问题描述】:
有谁知道如何快速裁剪具有给定角度的图像? 我把演示图像放在下面。 我google了一下,发现几乎所有的解决方案都是关于不旋转或90度旋转的图像。 我想旋转图像然后裁剪它,就像 iPhone 中的照片应用程序一样。 感谢您的任何提示!
【问题讨论】:
标签: ios swift image uiimage crop
有谁知道如何快速裁剪具有给定角度的图像? 我把演示图像放在下面。 我google了一下,发现几乎所有的解决方案都是关于不旋转或90度旋转的图像。 我想旋转图像然后裁剪它,就像 iPhone 中的照片应用程序一样。 感谢您的任何提示!
【问题讨论】:
标签: ios swift image uiimage crop
一种选择是使用 CGContext 和 CGAffineTransform 根据您的角度旋转。
制作两个矩形,一个用于旋转图像,一个用于裁剪图像并使用裁剪(to rect: CGRect) -> CGImage?
最后根据你的逻辑只制作一两张图片,这完全取决于你的方法。
这里有一个很好的参考:
https://www.raywenderlich.com/2305-core-image-tutorial-getting-started
希望对你有帮助
【讨论】:
设计故事板并在 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 创建扩展 — refer。缩放图片,使用委托函数 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。如果将picker相机捕获模式设置为照片。否则处理动作。然后将源类型设置为相机,将相机捕获模式设置为照片。
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
用于缩放为滚动缩放比例。
let scale = 1/scroll.zoomScale
然后设置裁剪区域框架(x, y, width, height)。
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!)
【讨论】: