我正在寻找一些指导,因为我建立了一个自定义相机。我在以前的项目中对照片使用了方形裁剪,但现在我需要创建一个自定义的裁剪形状。我应该使用路径还是掩码来执行此操作?
我需要添加裁剪的图像到另一个图像,并生成一个png图像与两个图像相结合。请参考照片的想法,我正在寻找建立。
目前正在构建bezier路径,但不确定如何使用路径形状来裁剪图像:
func createBezierPath() -> UIBezierPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: 2, y: 26))
path.addCurve(to: CGPoint(x: 0, y: 12), // ending point
controlPoint1: CGPoint(x: 2, y: 14),
controlPoint2: CGPoint(x: 0, y: 14))
path.addLine(to: CGPoint(x: 0, y: 2))
path.addArc(withCenter: CGPoint(x: 2, y: 2), // center point of circle
radius: 2,
startAngle: CGFloat(M_PI), // π radians = 180 degrees = straight left
endAngle: CGFloat(3*M_PI_2), // 3π/2 radians = 270 degrees = straight up
clockwise: true) // startAngle to endAngle goes in a clockwise direction
path.addLine(to: CGPoint(x: 8, y: 0))
path.addArc(withCenter: CGPoint(x: 8, y: 2),
radius: 2,
startAngle: CGFloat(3*M_PI_2), // straight up
endAngle: CGFloat(0), // 0 radians = straight right
clockwise: true)
path.addLine(to: CGPoint(x: 10, y: 12))
path.addCurve(to: CGPoint(x: 8, y: 15), // ending point
controlPoint1: CGPoint(x: 10, y: 14),
controlPoint2: CGPoint(x: 8, y: 14))
path.close()
return path
}
发布于 2017-01-29 11:02:14
将原始照片放入UIImageView
中,然后执行以下操作:
let mask = CAShapeLayer()
mask.path = createBezierPath()
imageView.layer.mask = mask
并且它应该只显示内部裁剪的面。如果您想要一个新的UIImage
,请使用renderInContext()
方法。
https://stackoverflow.com/questions/41918712
复制相似问题