贝塞尔路径的基本UIImageView蒙版形状
问题描述:
我想使个人资料照片看起来像这张图片。 我正在试验UIBezierPaths(),我相信它们是我最好的选择。贝塞尔路径的基本UIImageView蒙版形状
我也是新来的的iOS绘图,但到目前为止,我已经能够达到这个形状:
extension UIImage {
class func shapeImageWithBezierPath(bezierPath: UIBezierPath, fillColor: UIColor?, strokeColor: UIColor?, strokeWidth: CGFloat = 0.0) -> UIImage! {
//: Normalize bezier path. We will apply a transform to our bezier path to ensure that it's placed at the coordinate axis. Then we can get its size.
bezierPath.apply(CGAffineTransform(translationX: -bezierPath.bounds.origin.x, y: -bezierPath.bounds.origin.y))
let size = CGSize(width: bezierPath.bounds.size.width, height: bezierPath.bounds.size.height)
//: Initialize an image context with our bezier path normalized shape and save current context
UIGraphicsBeginImageContext(size)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
context.saveGState()
//: Set path
context.addPath(bezierPath.cgPath)
//: Set parameters and draw
if strokeColor != nil {
strokeColor!.setStroke()
context.setLineWidth(strokeWidth)
} else { UIColor.clear.setStroke() }
fillColor?.setFill()
context.drawPath(using: .fillStroke)
//: Get the image from the current image context
let image = UIGraphicsGetImageFromCurrentImageContext()
//: Restore context and close everything
context.restoreGState()
UIGraphicsEndImageContext()
//: Return image
return image
}
}
let circleShapePath = UIBezierPath()
circleShapePath.move(to: CGPoint(x: 50, y: 0))
circleShapePath.addLine(to: CGPoint(x: 100, y: 0))
circleShapePath.addLine(to: CGPoint(x: 100, y: 100))
circleShapePath.close()
let newCircle = UIImage.shapeImageWithBezierPath(bezierPath: cirleShapePath, fillColor: .red, strokeColor: .white, strokeWidth: 5)
我的麻烦已经找到顶端左侧和底部点用曲线连接,并将其桅杆一个UIImageView正确的方式。
谢谢!