How to crop the image using UIBezierPath?

2020-01-27 12:00发布

I want to get the image from the UIBezierpath closed path(See the image). I draw the image on the UIView using drawRect method and also draw the lines are using the drawRect method. How can I get the particular closed path image? Please help me. Thanks in advance.

This code used for draw the bezierpath.

UIBezierPath *aPath = [UIBezierPath bezierPath];
for (NSString *pointString in pointArray) {
    if ([pointArray indexOfObject:pointString] == 0)
        [aPath moveToPoint:CGPointFromString(pointString)];
    else
        [aPath addLineToPoint:CGPointFromString(pointString)];
}
[aPath closePath];

enter image description here

8条回答
唯我独甜
2楼-- · 2020-01-27 12:41

For Swift try this : ZImageCropper

ZImageCropper is using following things as core part:

  • UIBezierPath
  • UITouch Events
  • CAShapeLayer
  • CoreGraphics
查看更多
Anthone
3楼-- · 2020-01-27 12:41

iDev's answer is excellent, but if you need transparent background, then except UIGraphicsBeginImageContext(view.bounds.size); you should use

UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0);

By default image context is created with opaque == YES, so you have to change it to NO.

查看更多
登录 后发表回答