How to crop the image using UIBezierPath?

后端 未结 8 1617
我在风中等你
我在风中等你 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:49

    For Swift try this : ZImageCropper

    ZImageCropper is using following things as core part:

    • UIBezierPath
    • UITouch Events
    • CAShapeLayer
    • CoreGraphics
    0 讨论(0)
  • 2020-11-30 23:53

    use this pod ios-helpers

    - (UIImage *)imageWithStrokeColor: (UIColor *)stroke_color andFillColor: (UIColor *)fill_color {
    
        // adjust bounds to account for extra space needed for lineWidth
        CGFloat width = self.bounds.size.width + self.lineWidth * 2;
        CGFloat height = self.bounds.size.height + self.lineWidth * 2;
        CGRect bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, width, height);
    
        // create a view to draw the path in
        UIView *view = [[UIView alloc] initWithFrame:bounds];
    
        // begin graphics context for drawing
        UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
    
        // configure the view to render in the graphics context
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        // get reference to the graphics context
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        // reverse the y-axis to match the opengl coordinate space
        CGContextScaleCTM(context, 1, -1);
        CGContextTranslateCTM(context, 0, -view.bounds.size.height);
    
        // translate matrix so that path will be centered in bounds
        CGContextTranslateCTM(context, -(bounds.origin.x - self.lineWidth), -(bounds.origin.y - self.lineWidth));
    
        // stroke color
        [stroke_color setStroke];
        [self stroke];
    
        //fill color
        [fill_color setFill];
        [self fill];
    
        // get an image of the graphics context
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    
        // end the context
        UIGraphicsEndImageContext();
    
        return viewImage;
    }
    
    0 讨论(0)
提交回复
热议问题