iPhone剪辑图像与路径

hug*_*gie 6 iphone quartz-graphics

我想用路径剪辑图像.在"使用石英编程"一书中,有一个关于如何绘制由矩形路径剪切的圆的示例(第37页),还有一章关于图像蒙版,现有图像为模板(第10章).但我仍然不确定如何使用路径剪辑现有图像.有没有例子或指针?

kra*_*nyk 13

这是一个应该工作的示例,它将剪切区域设置为路径(在我的情况下,此路径是一个elipse,您可以使用rect).然后绘制将被剪裁的图像.方法drawRect:是在我的情况下绘制UIView的上下文的方法.


- (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]];
}