How to crop the image using UIBezierPath?

后端 未结 8 1671
我在风中等你
我在风中等你 2020-11-30 23:16

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 als

8条回答
  •  隐瞒了意图╮
    2020-11-30 23:34

    You can use a shapeLayer for that. Something like,

    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];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = aPath.CGPath;
    [view.layer setMask:shapeLayer];//or make it as [imageview.layer setMask:shapeLayer]; 
    //and add imageView as subview of whichever view you want. draw the original image
    //on the imageview in that case
    

    To get it as an image,

    UIGraphicsBeginImageContext(view.bounds.size);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    image should have the corresponding image.

提交回复
热议问题