How to crop the image using UIBezierPath?

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

    this works for me...

    UIBezierPath *path = ....//your path
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef    context    = CGBitmapContextCreate(nil, frame.size.width, frame.size.height, 8, 4*frame.size.width, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
    
    CGContextAddPath(context, path.CGPath);
    CGContextClip(context);
    CGContextDrawImage(context, frame, image.CGImage);
    
    [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    

提交回复
热议问题