【问题标题】:Crop Photo with Custom Shape使用自定义形状裁剪照片
【发布时间】:2017-06-14 14:17:19
【问题描述】:

在构建自定义相机时,我正在寻求一些指导。我在以前的项目中对照片使用了方形裁剪,但现在我需要创建一个自定义裁剪形状。我应该使用路径还是蒙版来执行此操作?

我需要将裁剪后的图像添加到另一张图像中,并生成一张将两张图像组合在一起的 png 图像。请参考照片以了解我要构建的内容。

目前一直在构建贝塞尔路径,但不知道如何使用路径形状来裁剪图像:

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
}

【问题讨论】:

  • 你好@Paul k。我必须在我的应用程序中做同样的事情(根据上面的图像仅捕获面部并保存)在目标 c 所以我必须做的如果你有任何类型的库只能捕获面部并保存,你能给我链接吗?帮助我..提前谢谢
  • hi.sir 帮助我的项目我尝试裁剪面部形状图像但不裁剪完美的面部形状图像请帮助我客观 c..

标签: objective-c swift camera png crop


【解决方案1】:

将原始照片放入UIImageView 并执行以下操作:

let mask = CAShapeLayer()
mask.path = createBezierPath()
imageView.layer.mask = mask

它应该只显示内部裁剪的脸。 如果您想拥有一个新的UIImage,请使用renderInContext() 方法。

【讨论】:

  • 谢谢阿利斯特拉!我会试试这个。
  • 你好@Alistra我必须在我的应用程序中做同样的事情(根据上面的图像仅捕获面部并保存)在目标c中所以我必须做什么你能给我链接如果你有任何一种仅用于捕获面部并保存的库,所以请帮助我..提前感谢
猜你喜欢
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多