4

我有一个UITableViewCell带有UIImageas 属性的子类。
我想在 drawRect 中绘制图像,并且要高效,因为这是一个表格视图单元格。

我知道如何按原样绘制图像:

-(void)drawRect:(CGRect)aRect 
{
    CGRect rect = self.bounds;

    [self.image drawInRect:rect];
} 

但是我怎样才能用圆角绘制图像并保持效率呢?

顺便说一句,我不想​​使用UIImageView然后设置图层角半径,因为它接缝会损害性能。

4

1 回答 1

16

您需要创建一个剪切路径:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath;
CGContextAddPath(ctx, clippath);
CGContextClip(ctx);
[self.image drawInRect:rect];
CGContextRestoreGState(ctx);
于 2012-05-30T10:51:27.597 回答