iPhone clip image with path

2020-02-10 08:10发布

I would like to clip an image with path. In the book Programming with Quartz there is an example on how to draw a circle clipped by a rectangular path (p.37), and there is also a chapter on image masking with existing image as stencil (Ch.10). But I'm still not sure about how to clip an existing image using path. Is there any example or pointer?

1条回答
来,给爷笑一个
2楼-- · 2020-02-10 08:22

Here is a sample that should work, it sets the clipping area as a path(this path in my case is an elipse, you could use a rect). Then the image that will be clipped is drawn. Method drawRect: is the one that draws the UIView's context in my case.


- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGMutablePathRef path = CGPathCreateMutable();
//or for e.g. CGPathAddRect(path, NULL, CGRectInset([self bounds], 10, 20));
    CGPathAddEllipseInRect(path, NULL, [self bounds]);
    CGContextAddPath(context, path);
    CGContextClip(context);
    CGPathRelease(path);
    [[UIImage imageNamed:@"GC.png"] drawInRect:[self bounds]];
}

查看更多
登录 后发表回答