2017-04-25 90 views
0

我想使個人資料照片看起來像這張圖片。 我正在試驗UIBezierPaths(),我相信它們是我最好的選擇。貝塞爾路徑的基本UIImageView蒙版形狀

enter image description here

我也是新來的的iOS繪圖,但到目前爲止,我已經能夠達到這個形狀:

enter image description here 對於我已經能夠學習。代碼:

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正確的方式。

謝謝!

回答